So basically, what I want to do is to use “if / else” as switches for program to choose a path.
I want more indented if / else NOT to work, if my elif statement was false. Is it possible?
Code:
aStr = "abcdef"
char = "e"
if aStr == '':
return False
elif len(aStr) == 1:
if char == aStr:
return True
else:
print "Python is stupid"
return False
else:
And it returns “Python is stupid”, which I don’t meant it to do!
If you want it to run when it’s not true, you need to reverse your logic in the
elif:The
if(and by extension,elif) statement runs the block if the given expression is true. If you want to check if something isn’t true, you reverse the logic (putting anotin front of it, or using the inverse operator).Note that the Pythonic way to check for an empty string is to do
if not aStr:, rather than check for equality vs an empty string.