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")
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
+ 2
Which task is it? (Number?)
Actually you can use else in Python try block:
https://www.askpython.com/python/python-exception-handling
https://code.sololearn.com/cltb0XimZXsI/?ref=app
+ 2
The try block should contain code that might throw an exception. In your code, it is not fulfilled.
+ 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.
+ 1
Lisa didn't know that. Thanks!
+ 1
You input should be in the try block
0
Thank you all for your feedback....