I'm attempting to create a small test script that appends something to a note. Contained below is the main function that I will execute in the script. The problem seems to be that I can't get the else
block to run when the while
block evaluates to false (that is to say, when it evaluates to anything that isn't one of those four options), the while
block just continues in an infinite loop. I've also attempted to insert a break
into the while loop but this terminates the script after the while
loop has executed.
How do I move from the while
to the else
block when it evaluates to false? And why doesn't the current way I do things work as I would like it to? Thanks.
def start():
q01 = input("What is the subject of your note?\n")
q02 = input("Are you certain that the subject of your note is " + q01 + "?\n")
while q02 == 'No' or 'no' or 'NO' or 'n':
q01 = input("So, what is the subject of your note?\n")
q02 = input("Are you certain now that the subject of your note is " + q01 + "?\n")
else:
q03 = Enter("Enter the content of your note")
Your culprit is the while loop condition:
while q02 == 'No' or 'no' or 'NO' or 'n':
This is equivalent to:
while (q02 == 'No') or 'no' or 'NO' or 'n':
As 'no'
, 'NO'
and 'n'
are all non-empty strings they evaluate to True
and so your condition evaluates to:
while (q02 == 'No') or True or True or True:
which is clearly always True
.
To fix this you need to adjust the condition to:
while q02 == 'No' or q02 == 'no' or q02 == 'NO' or q02 == 'n':
Although to be more pythonic you could instead make this:
while q02 in ['No','no','NO','n']: