+ 2

Where to put Error? ValueError?

I try to put except for "x" and "y", i think it should be a ValueError, cose i want my program to run, print: "it can't be a string it have to be integer" and ask again for first number. My code is: import time print("hello") time.sleep(5) x=int(input("first number: ") y=int(input("second number") print(x+y) if x+y <= 20: print("not bad") elif x+y >= 21: print("good") time.sleep(3) print("goodbye") I tried to put "try:", "except ValueError:", "print("it can't be a string it have to be integer" everywhere but it doesn't work...

6th Sep 2020, 6:42 PM
Martyna Tomaszewska
Martyna Tomaszewska - avatar
2 Réponses
+ 4
while True: try: a=int(input()) break except ValueError: print("it can't be a string ,it has to be am integer") print(a) Input "6" 5 works fine and prints it can't be a string and then the correct output
6th Sep 2020, 6:46 PM
Abhay
Abhay - avatar
+ 1
Done! thank You:)
6th Sep 2020, 6:53 PM
Martyna Tomaszewska
Martyna Tomaszewska - avatar