Python Forum
Unexpected input within a while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected input within a while loop
#1
In this preliminary while loop, the user is asked to input an advantage. If they input 1,2, or 3 they while loop should say "Your stats been updated" and it does. But it also does for anything else, which I don't want it to do. Any help is appreciated.

valid = {'1', '2', '3'}
advantage = " "
while advantage not in valid:
    advantage = input("Do you pick extra health [1], extra luck [2], or extra power [3]")

    break

print ("Your stats have been updated")
Thanks in advance
Reply
#2
remove the break
Reply
#3
Larz60+ solution woks, but just for completeness, here is an example of the loop using the break. You mix the two approaches in your code
valid = {'1', '2', '3'}
while True:
    advantage = input("Do you pick extra health [1], extra luck [2], or extra power [3]")
    if advantage in valid:
        break
print ("Your stats have been updated")
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  If 2nd input in the game is correct, replace with 1st input and loop the game tylerdurdane 11 4,030 Jul-17-2022, 04:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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