Python Forum
Unexpected input within a while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Unexpected input within a while loop (/thread-10086.html)



Unexpected input within a while loop - fier259 - May-12-2018

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


RE: Unexpected input within a while loop - Larz60+ - May-12-2018

remove the break


RE: Unexpected input within a while loop - buran - May-14-2018

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