Python Forum
while loop issue, help please?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop issue, help please?
#1
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()
Reply
#2
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Reply
#4
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 >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply
#6
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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
That was the problem! I have changed the second if statement to
elif
. Thank you so much for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For List Loop Issue Galdain 2 2,053 Dec-31-2019, 04:53 AM
Last Post: Galdain
  issue with updating list every iteration of a loop ftrillaudp 2 3,069 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  for-loop issue digysol 2 2,093 Oct-24-2018, 06:12 PM
Last Post: nilamo
  Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop) Placebo 2 3,514 Sep-30-2018, 01:19 PM
Last Post: Placebo
  while loop issue - stuck in the loop! EricMichel 6 8,615 Aug-06-2018, 03:59 PM
Last Post: EricMichel
  Loop issue onenessboy 11 6,749 Mar-03-2018, 08:40 AM
Last Post: onenessboy
  While loop issue CWatters 4 3,818 Sep-25-2017, 08:58 PM
Last Post: nilamo
  Code issue with time remaining loop. Python3 deboerdn2000 11 8,847 May-04-2017, 04:53 PM
Last Post: deboerdn2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020