I need to write an if statement that will take user input and accept integers.
Here is the code, but it is only taking the specified integers.
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
I need to write an if statement that will take user input and accept integers.
Here is the code, but it is only taking the specified integers.
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
There are a couple approaches to this question since I'm not to sure what exactly your looking for. Here they are:
If you want them to be not an integer and you aren't interested in converting the input to an integer check it out below I used the isnumeric() function as stated above:
Python 3 answer below:
enter_num=input('Please Enter a number: \n') # I am prompting the user #for input and the \n means new line so the user would enter a value on #the new line
if enter_num.isnumeric(): #if the value the user entered is a number #proceed into the if statement
print('You must have entered an integer value') # print this #statement out if the if statement conditon is true
I'm not sure if you are familiar with try blocks so I won't get into it but here is another approach(im also not sure if you are familiar with the isinstance() function):
number=int(input('Please Enter a number: \n')) #prompting the user for #a number
if isinstance(number,int): # if the vlaue is a integer enter the if #statement
print('this is an integer') #enters the if statement if its true
I'm sorry if my solution helped. But there is also this question which might help: How do I check if raw input is integer in python 2.7?