0

How to compare blank integer input

otp =6773 while True: myotp=int(input(' Please enter the otp: ')) if myotp==: print('Dont keep otp as blank') continue else: print('I am inside') In this integer case , it returns error . Eg - If I don't fill the OTP and keeping blank then be pressing enter . It should print don't keep OTP as blank (like that i want it ) . But in string case it is working correctly . But in integer case not . PLZ help me otp ='6773' while True: myotp=input(' Please enter the otp: ') if myotp=='': print('Dont keep otp as blank') continue else: print('I am inside')

28th Jun 2022, 2:36 AM
Neha Sharma
11 Respuestas
+ 3
The way explained above. The input() function returns a string by default. A string is a string or Nonetype, even when it's empty . You can only convert number strings to integers. An empty string is not a number. Take input, check if it's a digit, if it is, change it to an integer, if not, do whatever
28th Jun 2022, 3:16 AM
Slick
Slick - avatar
+ 2
You could also do a try and except block, catch the error thats thrown if letters are entered, and print your message and continue
28th Jun 2022, 3:19 AM
Slick
Slick - avatar
+ 2
Neha Sharma Why don't you want to take a string as an input? It is an easy way to check, abd convert for the rest if your code
28th Jun 2022, 7:41 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Slick mentioned using a try/except statement to validate the input. It would look like this: # The following will test your input to be an integer. try: num = int(input()) num + 7 # Your code goes here except: print('Your input is not an integer')
28th Jun 2022, 8:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Well first off it's incorrect syntax. And then if you fix it by adding a blank string after the == then it will still cause an error because you cannot convert a string character (or lack of one) to an integer with int(). You'd need to take input as a string. Then you can see if it's a number with <string>.isdigit(). If it is, convert to integer.
28th Jun 2022, 3:08 AM
Slick
Slick - avatar
+ 1
Because I want to know whether it can be possible or not . If it is possible there what will the logic
28th Jun 2022, 7:49 AM
Neha Sharma
0
See what I want is that in taking input as a string it works correctly by doing myotp==''. But I don't want to use string instead of integer how how I can write myotp== ????? . How to write that logic
28th Jun 2022, 3:11 AM
Neha Sharma
0
Can you send me the code . I hope so what I am trying to say ( my query ). I want that logic. Without converting integer into string it will not work . Thankyou for response
28th Jun 2022, 3:18 AM
Neha Sharma
0
I will explain you step by step Eg - In string case we can compare like this When myotp takes string input . But even if I don't fill anything then also below code can check condition myotp=='' But this logic won't work when it is Integer and I don't want to even convert to Integer to string . How i should I write the code . Eg - myotp=int(input (" Enter your OTP : ")) myotp is taking integer input but I didn't fill anything and pressed enter . If I didn't fill anything it means it is NAN ( Not a number ) but can be a string. As it is mentioned int so it will even not consider String . So PLZ provide me the code
28th Jun 2022, 3:28 AM
Neha Sharma
0
Ok . I GET IT . TYSM (THANKYOU SO MUCH EVERYONE )
28th Jun 2022, 11:40 AM
Neha Sharma
0
Hdld
29th Jun 2022, 3:29 PM
mahesh kote
mahesh kote - avatar