Reputation: 9
When I run the following code, I get an error: "IndentationError: unident does not match any outer indentation level".
What am I doing wrong? I've included my code below for reference:
file=open('csquetionseasy.txt','r')
print(file.read(432))
answersinputeasy=input('enter the letter responding to the correct answer for all 3 questions e.g. BBB')
if answersinputeasy==('BAA'):
print('you got 3/3!')
else:
if answersinputeasy==('BAB'):
print('2/3!')
else:
if answersinputeasy==('BBB'):
print('1/3!')
else:
if answersinputeasy==('ABB'):
print('0/3!')
else:
if answersinputeasy==('AAB'):
print('1/3!')
else:
if answersinputeasy==('AAA'):
print('2/3!')
else:
if answersinputeasy==('ABA'):
print('1/3!')
Upvotes: 0
Views: 1195
Reputation: 1297
The reason that you are getting the error "IndentationError: unident does not match any other indentation level"
is because you are chaining tabs together to create a nested logic statement (in pseudo-code):
if <condition>:
#then do something
else if <condition>:
#then do something else
else if <condition>
#then do something further else
This is not how Python likes to see syntax in a logical block. Additionally, the next error you're going to run into will surround the use of nested if statements inside of else clauses.
To run an else if
statement in Python, you will want to use the syntax elif:
followed by an indented line with the code that you want to execute if that condition is met (in pseudo-code):
if <condition>:
#then do something
elif <condition>:
#then do something else
elif <condition>:
#then do something further else
One other call out as a best practice, is that you should include an explicit else
clause in a conditional block with a bunch of elif
statements if you're not going to do any further validation on the string you're getting from the user. Imagine that the user passed in XYZ
. They wouldn't meet any of the conditions you've defined, and because of that the code would just continue out of the bottom of this logic block (which may or may not be a good thing). In the following code, I've added an example of what an explicit else
clause might look like, but it's up to you to ultimately decide what might make sense for your application:
file=open('csquetionseasy.txt','r')
print(file.read(432))
answersinputeasy=input('enter the letter responding to the correct answer for all 3 questions e.g. BBB')
if answersinputeasy==('BAA'):
# Code to be executed following an if-statement must be indented by 4 spaces:
print('you got 3/3!')
elif answersinputeasy==('BAB'):
# Code to be executed following an elif-statment (else-if) must be indented by 4 spaces:
print('2/3!')
elif answersinputeasy==('BBB'):
print('1/3!')
elif answersinputeasy==('ABB'):
print('0/3!')
elif answersinputeasy==('AAB'):
print('1/3!')
elif answersinputeasy==('AAA'):
print('2/3!')
elif answersinputeasy==('ABA'):
print('1/3!')
# Else clause would provide a default condition that executes if none of the prior cases are met.
else:
# Code following an else statment must be indented by 4 spaces:
#Example of a default Else-block entry:
print('Wasn\'t able to parase your entry into a valid answer!')
Upvotes: 1
Reputation: 184
Use elif
rather than else
. You need else
statement to do something when if
and elif
statements evaluate to false.
if answersinputeasy==('BAA'):
print('you got 3/3!')
elif answersinputeasy==('BAB'):
print('2/3!')
elif answersinputeasy==('BBB'):
print('1/3!')
elif answersinputeasy==('ABB'):
print('1/3!')
elif answersinputeasy==('AAA'):
print('2/3!')
elif answersinputeasy==('ABA'):
print('1/3!')
else:
print('Invalid Input')
Also, if you want to indicate a block of code, you must indent each line of the block by the same amount, which is typically four spaces.
Upvotes: 6