Python

Python error IndentationError expected an indented block

BA Asked by Babita · 22-02-2020
0 upvotes 360 views 0 comments
The question
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

5 answers

0
JO
Answered on 24-08-2022

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!!

0
KA
Answered on 24-02-2023
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.
0
KO
Answered on 22-03-2023
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.
0
AR
Answered on 27-05-2023

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
0
BH
Answered on 23-08-2023

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()

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session