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

8th Dec 2020, 4:48 PM
Guido Nocella
Guido Nocella - avatar
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/
8th Dec 2020, 5:03 PM
Simba
Simba - avatar
+ 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
8th Dec 2020, 5:05 PM
Ratnapal Shende
Ratnapal Shende - avatar
+ 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.")
8th Dec 2020, 5:05 PM
Yasara hasini
8th Dec 2020, 5:21 PM
Shadoff
Shadoff - avatar
+ 1
Thank you very much!
8th Dec 2020, 5:30 PM
Guido Nocella
Guido Nocella - avatar