0
How to print a message if the input is a word?
Hi, maybe it's a stupid question, but this is my third day in Pyhton. I want to print some kind of message like "Only numbers. No words" when the input is a letter (or letters) instead of a number in this little program. How can I modify the code? You can see the code here: https://code.sololearn.com/c39yE44Vz0EN/#py
5 Answers
+ 6
You can use try/except statement
try:
your code
except:
print("I said numbers between 1 and 10!")
print("Game Over")
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2441/
+ 3
you can use elif statements instead of lot of if if if blocks..
use error handling to find out the wrong input when user entered...
I did a little modification in your code see here :
https://code.sololearn.com/cp3a7cHNw0NX/?ref=app
+ 1
while True:
input_number = input()
if input_number.isdigit():
input_number=int(input_number)
if input_number == secret_num:
print("Well done " + player_name)
break
if input_number > secret_num:
print("too big!")
if input_number < secret_num:
print("too small!")
if input_number > 10:
print("I said between 1 and 10!")
else:
print("Numbers only.No words.")
+ 1
for example, like this:
https://code.sololearn.com/cXUQluvvc8AB/?ref=app
+ 1
Thank you very much!