Why Won't The While Loop Continue And Stop After A User Question?
Solution 1:
Your problem is that:
if quitter == "yes"or"Yes"
is based on a misunderstanding of Python logic statements.
It is not interpreted as if quitter == ("yes"or"Yes")
as you intended, but it is actually interpreted as:
if (quitter == "yes") or ("Yes")
Any non-blank string evaluates to True, so the break
is always executed.
A better way of writing this would be:
if quitter.lower() == "yes":
This converts the contents of quitter
to lower case and checks that, so you don't need to check upper and lower case, and other variants like "YES".
Solution 2:
Please change this statement to as below :
if ((quitter == "yes") or (quitter == "Yes")):
Solution 3:
After prompting the user if they want to quit, change the if statement to
ifquitter== "yes"orquitter== "Yes":
breakelse:
continue
The reason the program exits is because it's evaluating the string 'Yes'
on it's own as True which means the or statement evaluates as True
and thus break
is ran. You have to specify quitter both before and after the or statement.
truth_val = bool('Yes')
print(truth_val)
>>>True
Post a Comment for "Why Won't The While Loop Continue And Stop After A User Question?"