Is there a way to allow only integers except one specific string? I wrote a number guessing game in python and if the player decides to quit the game, the game should break through the user input "quit", while allowing only numbers during the guessing process. Thank you in advance!
Asked
Active
Viewed 252 times
-2
-
1The input is a string at first anyway. Check it for "quit", if not, convert to int and proceed. – Michael Butscher Apr 04 '20 at 14:18
-
1@MichaelButscher that would break if the user typed something that is not an int and not 'quit', might want to check if it is numeric before converting. – Eric Apr 04 '20 at 14:19
-
Can you clarify what exactly the issue is? Are you familiar with basic control flow? – AMC Apr 04 '20 at 14:25
-
Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Apr 04 '20 at 22:51
2 Answers
2
Check if input is digit and not 'quit' then continue game
print("Insert your number")
user_input = input()
if user_input.isdigit():
#continue your game
elif user_input == 'quit':
#quit game
else:
print("Invalid option. Type in a number or 'quit' for exit")

Alex Susanu
- 163
- 1
- 9
1
how about just checking for either numeric or 'quit'?
if input.isnumeric():
#proceed as with number input
elif input == 'quit':
#quit
else:
#invalid input

Eric
- 1,210
- 8
- 25