This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Identifying the data type of an input
(4 answers)
Closed 4 years ago.
I am trying to check if the input is integer. But the code is repetedly saying "something else" in each and every input I give. Is something wrong with the code?
x = input("enter:")
if type(x) == int:
print("int")
else:
print("something else")
if float(x).is_integer():
# do stuff
note, as the comments have said, x here is a string, so you're converting that string to a number first, then checking if it's an integer.
you may also want to wrap this around a try block to catch strings that are not numbers, etc.
Related
This question already has answers here:
Identifying the data type of an input
(4 answers)
Closed 2 years ago.
I'm unable to compare users input as str or int in following code..
A=input(" ")
if type(A)=str:
Print("this is string")
elif type(A)=int:
Print("this is integer")
So please tell me how to deal with this type of problem
use string class isdigit() method to check user input is number or string.
This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 3 years ago.
I keep getting this error:
if AR_GCC<16 and AR_GCC>0:
TypeError: '<' not supported between instances of 'str' and 'int'
It is a program that is supposed to print the cancer risk according to the input of the user (not a real thing just for practice)
This is the piece of code that doesn't work:
AR_GCC=input("AR_GCC repeat copy number?")
if (AR_GCC>0 and AR_GCC<16):
risk="High risk"
elif (AR_GCC >= 16):
risk = "Medium risk"
else:
print("Invalid")
You need to make sure the input is interpreted as number. Simply adding int() before the input function should solve that. Of course if the value passed is a string, it will fail. Therefore your code should be:
AR_GCC=int(input("AR_GCC repeat copy number")) should do the trick. Sorry for formatting, typing from phone
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
Could someone please help me to understand why the program doesn't output 'Well done!' after I input 1? Thank you.
while True:
o=input("Enter 1:")
if o==1:
break
print("Well done!")
It looks like you're using python3.x.
On python3.x, input always returns an instance of str, so o will never equal the 1 since you're comparing different types (str and int).
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I'm working on a Python 3.4 coding assignment that needs an integer input from the user. I am having trouble figuring out how to make the program accept only integer values; for instance, if the user inputs a float (i.e. "9.5"), the program will output, "That's not an integer! Try again."
Simple, use raw_input to get the string input, then call .isdigit() on it to see if it is an int. If it is, cast it to an int, then check it is within the valid range. Stick it all in a while loop so it keeps being called until a valid number is input, and you're all set.
This question already has answers here:
How do I check if a string represents a number (float or int)?
(39 answers)
Closed 8 years ago.
I want a program to convert user input to an integer and store it in a list only if it can be. If the input is not able to be converted to an integer, the program should just continue and ignore the input.
The best approach here is to just try and convert it and ignore the error if the conversion fails. For example:
try:
your_list.append(int(user_input))
except ValueError:
pass