Python If--elif-else Usage And Clarification
Solution 1:
It's about the line if choice == 'x' or 'X'
.
Correctly, it should be
if choice == 'x' or choice == 'X'
or simpler
if choice in ('X', 'x')
because the or operator expects boolean expressions on both sides.
The current solution is interpreted as follows:
if (choice == 'x') or ('X')
and you can clearly see that 'X'
does not return a boolean value.
Another solution would be of course to check whether if the uppercase letter equals 'X' or the lowercase letter equals 'x', which might look like that:
if choice.lower() == 'x':
...
Solution 2:
Your problem is with your if choice == 'x' or 'X':
part.To fix that change it to this:
if choice.lower() == 'x':
Solution 3:
ifchoice== 'x' or 'X':
is not doing what you think it's doing. What actually get's parsed is the following:
if (choice == 'x') or ('X'):
You probably want the following:
ifchoice== 'x'orchoice== 'X':
which can be written as
if choice in('x', 'X'):
Solution 4:
As the interpreter says, it is an IndentationError. The if statement on line 31 is indent by 4 spaces, while the corresponding else statement is indent by 5 spaces.
Post a Comment for "Python If--elif-else Usage And Clarification"