Python Forum

Full Version: while loop issue, help please?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am new to Python (and coding) and having an issue with a while loop (see code below). When I get to the end of my loop, I prompt the user to enter 1, 2, or 3 to continue playing (1 and 2) or quit (3). Selecting 2 (continue to play and clear the screen) and 3 (quit) both work, but when I enter 1 I am given the quit message and the game ends. Can you help me identify the issue? (I only posted the code that applies and not the whole middle section...)
Thanks!

playAgain = True

while playAgain:
    print("Choose a mode")
    print("1) Rectangle Pattern")
    print("2) Circle Pattern")
    print("3) Super Pattern")
    mode = input("Which mode do you want to play? 1, 2 or 3: ")
.................
.................
.................
    # Play again?
    print("Do you want to play again?")
    print("1) Yes, and keep drawings")
    print("2) Yes, and clear drawings")
    print("3) No, I am all done")
    response = input("Choose 1, 2, or 3: ")

    if response == 1:
        playAgain = True
    if response == 2:
        pattern.reset()
        playAgain = True
    else:
        playAgain = False
        print("Thanks for playing!")
        pattern.done()
in python3 input returns str. In your if block (lines 19, 21) you compare user response to int. compare it to strings, e.g. if response == '1': (note the quotes around 1)
I tried using
response = eval(input("Choose 1, 2, or 3: ")
as well as int. Either way does not work. Neither does using '' around the number (treating it as a string). The 2 and the 3 options work, regardless. The 1 option does not.
Any other ideas?
Don't use eval. It's bad practise, unless you are certain what you pass as input. When you take user from input it can be dangerous - user can input malicious code and you will execute it.
Did you try what I suggested?
response = input("Choose 1, 2, or 3: ")

if response == '1':
    print('You entered 1')
else:
    print('You entered something else')
Output:
Choose 1, 2, or 3: 1 You entered 1 >>>
Output:
Choose 1, 2, or 3: 3 You entered something else >>>
response = int(input("Choose 1, 2, or 3: "))

if response == 1:
    print('You entered 1')
else:
    print('You entered something else')
Output:
Choose 1, 2, or 3: 1 You entered 1 >>>
Yes. I've tried:
response = input("Choose 1, 2, or 3: ")

if response == '1':
    playAgain = True
as well as
response = int(input("Choose 1, 2, or 3: "))

if response == 1:
    playAgain = True
No matter what I do, it always works for the 2 and 3 options. Never for the 1 option.
the second problem is that you have 2 if statements. at the moment if you select 1 (the snippets in your last post are OK) it will set playAgain == True. Then it will execute second if and because response == 1 it will execute the else part, effectively reset playAgain to False. Use if/elif/else

if response == '1':
    playAgain = True
elif response == '2':
    pattern.reset()
    playAgain = True
else:
    playAgain = False
    print("Thanks for playing!")
    pattern.done()
That was the problem! I have changed the second if statement to
elif
. Thank you so much for your help.