Python error IndentationError expected an indented block
def example(p,q):
a = p.find(" ")
b = q.find(" ")
str_p = p[0:a]
str_q = p[b+1:]
if str_p == str_q:
result = True else:
result = False
return result
And I get the folloeing error:
IndentationError: expected an indented block
2020-02-22 in Python by Babita
| 312,421 Views
Write a Comment
Your email address will not be published. Required fields are marked (*)
All answers to this question.
class Formula():
def interest(self, P, N, R):
self.money=P*N*R/100
return self.money
class Tamil(Formula):
def c1(self):
class_name=customer1.__class__.__name__
print(f"Enter the Details of {class_name}:")
P=float(input("Enter the principal amount:"))
N=int(input("Enter the number of years:"))
R=float(input("Enter the rate of interest:"))
self.amount = Mainkey.interest(P, N, R)
print(f"{class_name} will be receiving Rupees {self.money} as interest.")
print("**************")
class Madhu(Formula):
def c2(self):
class_name=customer1.__class__.__name__
print(f"Enter the Details of {class_name}:")
P=float(input("Enter the principal amount:"))
N=int(input("Enter the number of years:"))
R=float(input("Enter the rate of interest:"))
self.amount = Mainkey.interest(P, N, R)
print(f"{class_name} will be receiving Rupees {self.money} as interest.")
print("**************")
class Sathiya(Formula):
def c3(self):
class_name=customer1.__class__.__name__
print(f"Enter the Details of {class_name}:")
P=float(input("Enter the principal amount:"))
N=int(input("Enter the number of years:"))
R=float(input("Enter the rate of interest:"))
self.amount = Mainkey.interest(P, N, R)
print(f"{class_name} will be receiving Rupees {money} as interest.")
print("**************")
mainkey=Formula()
customer1=Tamil()
customer2=Madhu()
customer3=Sathiya()
customer1.c1()
customer2.c2()
customer3.c3()
Answered 2023-08-23 by Bhupendra
Hi Rhea, As everyone else has mentioned, python identifies a new block of code based on indentations. In this case your, all the lines under your function should be indented. Then only Python can understand that these lines are the body of the function.
Try modifying your code like this:
def example(p,q): a = p.find(" ") b = q.find(" ") str_p = p[0:a] str_q = p[b+1:] if str_p == str_q: result = True else: result = False return result
Answered 2023-05-27 by Arvindra
In Python, indentation serves to define the scope of code blocks associated with control flow statements, as well as class and function definitions. In contrast, other programming languages like C#, Java, and C/C++ utilize braces { } to delineate code blocks. Therefore, it is essential to adhere to proper indentation when coding in Python. For further information on the significance of indentation in Python, please refer to the following resource.
Answered 2023-03-22 by komal
An additional space or the omission of one where necessary will undoubtedly result in an error message. Common reasons for this error include:
- Neglecting to indent the statements within a compound statement.
- Failing to indent the statements in a user-defined function.
The error message "IndentationError: expected an indented block" suggests that there is an issue with indentation. This is often due to a combination of tabs and spaces. Indentation can consist of any consistent whitespace, but it is advisable to use four spaces for indentation in Python. While tabulation or varying numbers of spaces may function, they can lead to complications. Using tabs is particularly discouraged, as they can produce inconsistent spacing across different editors.
Are you ready to harness the potential of data? Enroll in our Data Science with Python Course to acquire the skills necessary for data analysis, visualization, and making informed, data-driven decisions.
Answered 2023-02-24 by katty
Python requires its code to be indented and spaced properly. Never mix tabs and spaces. If you're using tabs stick to tabs and if you're using space stick to space.
Also in your code, change this part:
if str_p == str_q: result = True else: result = False return result
To
return str_p == str_q
Hope it helps!!
Answered 2022-08-24 by Johnson