0

How do i write the try and except of this?

The input should be a number (ie not alphabets or dots and other stuff) and only between 10-20, Inputs above 20 or below 10 should also be in the except part(i think) z = x <=20 and x >= 10 #this is the condition print(”write a number”) x = int(input()) Try: x=int(input())#i dont know if i should add the conditions within this part or not and how Except: print(”wrong input”)#can i specify what kind of wrong input they gave etc?

14th Feb 2022, 5:59 PM
Lenoname
5 Answers
+ 5
try: n = int(input("enter a number bt 10 and 20")) assert 10 <= n <= 20 except: print("not allowed")
14th Feb 2022, 6:12 PM
Oma Falk
Oma Falk - avatar
+ 3
Oma Falk Why is there a bare except statement? It should be : except ValueError
17th Feb 2022, 12:50 AM
Gurseerit Singh Chahal ✓
Gurseerit Singh Chahal ✓ - avatar
+ 3
Gurseerit Singh Chahal You are absolutely right as well as Nboumakis
17th Feb 2022, 7:38 AM
Oma Falk
Oma Falk - avatar
+ 2
As a sidenote, you shouldn't rely on try-except structures for flow control and that includes using assertions to check whether the value is in a specific range. It makes your code hard to read and unpredictable for the next person to read it. Use an if statement instead. ALWAYS try to write clean, consistent and self-documenting code, and avoid "cool" or "clever" code, unless absolutely necessary. It makes everyone's job easier. Edit: My point is mostly made for professional environments or for code that has the possibility of being used for more than fun or more than once
16th Feb 2022, 8:58 AM
Nboumakis
0
Gurseerit Singh Chahal like this?: except ValueError : print(”Numbers only!”)
17th Feb 2022, 12:55 PM
Lenoname