98.2 help! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

98.2 help!

So this is the question im stuck with and i cant seem to figure it out even though i have added a valueerror to the code but it still doesn't work. Can someone help explain why To order a restaurant dish online, the user should enter the code of desired dish, which contains only digits. Write a program that will take the code as input, and output "Enter only digits" if it contains non-digit symbols, and output "Order accepted" if it doesn't. If the ordering process went well, the program also should output "Bon appetit". Sample Input 1437 Sample Output Order accepted Bon appetit This is the code that i have created: code = int(input()) #your code goes here try: print("Order accepted") except ValueError: print("Enter only digits") else: print("Bon appetit") This was the original code: code = input() #your code goes here try: except else

20th May 2021, 10:07 AM
Zhi Hong
Zhi Hong - avatar
4 Answers
+ 4
code = input() try: if code.isdigit(): print("Order accepted") else: raise ValueError except ValueError: print("Enter only digits") else: print("Bon appetit") You didn't add the main logic to your code.. The if-else in try is important to solve the problem.
20th May 2021, 10:16 AM
sarada lakshmi
sarada lakshmi - avatar
+ 2
Hey there, A bit late but liked to share my code! :) code = input() #your code goes here try: code == int(code) print("Order accepted") except ValueError: print("Enter only digits") else: print("Bon appetit")
15th Aug 2021, 7:41 AM
Da Silva Miguel
Da Silva Miguel - avatar
0
Zhi Hong You can also take input in try block to handle error try: code = input() dish = int(code) print ("Order accepted") print ("Bon appetit") except: print ("Enter only digits")
20th May 2021, 2:02 PM
A͢J
A͢J - avatar
0
You can also try this way(don't forget to type "import re" at the top): import re code = input() #your code goes here pattern = r"\A[0-9]+\Z" try: if re.match(pattern, code): print("Order accepted") else: raise ValueError except ValueError: print("Enter only digits") else: print("Bon appetit")
16th Dec 2021, 10:12 AM
Diachenko Volodymyr
Diachenko Volodymyr - avatar