+ 6

Python

I want to check the user input to be within range(1,100) and if not showing an error and continue to give an correct input. P.S: Without if-else , any other thing can be used ? Like exceptional handling?

9th Feb 2017, 3:22 PM
Mr.Robot
Mr.Robot - avatar
5 odpowiedzi
+ 15
You can achieve that with an if else statement and exception handling. again = True while again == True: try: user = int(input("please input your number")) except TypeError and ValueError: print("Your number has to be a number") else: if user in range(100): print("Well done") again = False else: print("Your number has to be between 1 and 100") again = True The while loop is there because if the user does not input a number between 1 and 100, it will start again.
9th Feb 2017, 3:55 PM
Gami
Gami - avatar
+ 15
No. It will have to check IF the number is in thst range. The exception handling just handle the errors like TypeErrors or ValueErrors but it can't check if a number is in a specific range.
9th Feb 2017, 3:58 PM
Gami
Gami - avatar
+ 6
Without help of If else , we can't achieve my question? @Gami
9th Feb 2017, 3:57 PM
Mr.Robot
Mr.Robot - avatar
+ 4
You can use exceptions but Im sure you will still need to use IF
9th Feb 2017, 3:55 PM
Cosme
Cosme - avatar
+ 4
Your question is: I want to check "IF" the user input... so I dont think so we can achieve it without IF
9th Feb 2017, 3:59 PM
Cosme
Cosme - avatar