+ 1

Printing messages when there is no input

Is there a way I can add if User_input1>=int(1001): print("Input must be less than 1000") And if User_input2>=int(251): print("Input must be less than 250") To this code and make it work, or get similar results? I’ve tried some variations not using try/except and some of them print only the first message or print both the input and the message. User_input1=input() if not User_input1: print("Please enter first value") else: print(User_input1) try: User_input2=input() print(User_input2) except: print("Please enter second value")

23rd Jun 2020, 9:27 PM
•—• • •-• ••• —- -•
•—• • •-• ••• —- -• - avatar
1 Réponse
+ 4
An empty string is falsey in python, so just use; if User_input1: as if not User_input1: Is an empty string you're negating it to true and if true making it false. You can also substitute a value during input if nothing is entered using; User_input1 = input() or value If input() results in an empty string then the value is returned to User_input1 Remember the input value will be of type string so if you're needing an int type then you need to convert to int(). if int(User_input1) >= 1001: ... if int(User_input1) >= 251: ...
24th Jun 2020, 12:10 AM
ChaoticDawg
ChaoticDawg - avatar