-1
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")
    questionnumber = questionnumber+1
    #print(questionnumber)

while color != "Red":
    userask=input("What color of the rainbow am I thinking of? ")
    color = userask

    if color == "Red":
        print("CORRECT")
    else:
        print("INCORRECT")
        questionnumber = questionnumber+1
        #print (questionnumber)

    if questionnumber>3:
        quitter=input("Do you want to quit? ")
        if quitter == "yes"or"Yes":
            break
        else:
            continue

So the idea is you have to guess the correct color which in this case is red. But after three attempts, the program asks the user if they want to quit; if yes, the while loop stops, and if no, the while loop continues for another three attempts. I can't seem to figure out why the loop ends regardless of the user's answer.

martineau
  • 119,623
  • 25
  • 170
  • 301
Halo Man
  • 5
  • 4

3 Answers3

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

David Buck
  • 3,752
  • 35
  • 31
  • 35
0

Please change this statement to as below :

if ((quitter == "yes") or (quitter == "Yes")):
vbhargav875
  • 837
  • 8
  • 15
0

After prompting the user if they want to quit, change the if statement to

if quitter == "yes" or quitter == "Yes":
            break
        else:
            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
Chris Greening
  • 510
  • 5
  • 14