0

Exiting a while loop by changing a variable not working?

Say user input defines variable A as "red", and I want to create a loop that keeps asking the user for input until they enter the correct thing, "blue". I can't get the program to go on to the next section after the loop when A is defined as "blue", it just keeps looping and asking for input. This doesnt work A = input("what color?\n> ") while(A != "blue"): print("Wrong, try again") A = input("what color?\n> ") else: print("ok") Sorry if I formatted this poorly or did anything else wrong.

21st Nov 2016, 4:27 PM
Max Moses
Max Moses - avatar
2 odpowiedzi
+ 2
Well, everything worked fine for me. And what exactly happens when you input 'blue'? Does it print "Wrong, try again"? Maybe this variant will work better: while True: A = input("what color?\n> ") if (A == 'blue'): print("ok") break else: print("Wrong, try again")
21st Nov 2016, 4:38 PM
donkeyhot
donkeyhot - avatar
0
That works the way I wanted it to, thank you. I think trying to end the loop with while(A != "blue") instead of a break within it was the issue. Before, putting "blue" was doing the same thing as anything else, saying "wrong, try again" and looping. Also, I'm unaware if it is possible to reply to answers
21st Nov 2016, 5:01 PM
Max Moses
Max Moses - avatar