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')
11 Answers
+ 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
+ 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
+ 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
+ 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')
+ 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.
+ 1
Because I want to know whether it can be possible or not . If it is possible there what will the logic
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
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
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
0
Ok . I GET IT . TYSM (THANKYOU SO MUCH EVERYONE )
0
Hdld