Skip to content Skip to sidebar Skip to footer

Why Won't The While Loop Continue And Stop After A User Question?

questionnumber=0 color=('Red') userask=input('What color of the rainbow am I thinking of? ') color = userask if color == 'Red': print('CORRECT') else: print('INCORRECT')

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?"