0

Ordering dish online (Python Core) else with exception...code almost works!Why?

Bizarre:this code works for checking all codes, all but when user enters 138H for code, it produces a mistake. Any idea why? code=int(input()) #your code goes here try: print("Order accepted") except TypeError: print("Enter only digits") else: print("Bon appetit")

27th Feb 2022, 4:29 PM
Cathyboum
7 Respuestas
+ 5
Cathyboum , (1) the line that takes the input including the conversion has to me moved as the first line inside the try block (2) the type of error for the except block has to be ValueError not TypeError
27th Feb 2022, 5:29 PM
Lothar
Lothar - avatar
27th Feb 2022, 4:55 PM
Lisa
Lisa - avatar
+ 2
The try block should contain code that might throw an exception. In your code, it is not fulfilled.
27th Feb 2022, 5:25 PM
JaScript
JaScript - avatar
+ 1
DISREGARD POINT 1. FOR THE REASON SEE BELOW 1. try-except logic has no else clause. It has a finally clause (but it works very differently from an else clause). The closest to an else clause is an except clause without specific error type. 2. Your try-except logic does not catch errors in the input because you take input before the try clause. In fact it only catches errors in the first print statement and there are none.
27th Feb 2022, 4:44 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Lisa didn't know that. Thanks!
27th Feb 2022, 4:59 PM
Simon Sauter
Simon Sauter - avatar
+ 1
You input should be in the try block
27th Feb 2022, 9:03 PM
Pythøñ Cúltîst⚔️🔥🇳🇬
Pythøñ  Cúltîst⚔️🔥🇳🇬 - avatar
0
Thank you all for your feedback....
2nd Mar 2022, 2:45 PM
Cathyboum