How to code Python to accept only integers? [duplicate] - python

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.

Related

how to decide type of a variable in python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
enter image description here
I wrote a simple code in python.
Originally my assignment is to receive two inputs(name of person) and print them.
Here's my question.
When I try to sum two variables but one of them is int and another one is str, an error occurs.
But in this case (the picture) why variable 'a' is recognized as a str not int?
I think there must occurs an error but a is recognized as a str and work well.
In Python 3, input() always returns a string. (In Python 2, input() would try to interpret – well, evaluate, actually – things, which was not a good idea in hindsight. That's why it was changed.)
If you want to (try to) make it an int, you'll need int(input(...)).

How to compare input given by user with integer or string in python [duplicate]

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.

How to give input in any Data Types in Python? [duplicate]

This question already has answers here:
Can the input() function in Python dynamically detect the input's data type?
(3 answers)
Closed 2 years ago.
I have one doubt in the Python Input method.
When I am entering input, it is always considering as String in Python. How to get the Input value in many data types.
As Example:
If I enter Integer value as input then the Code supposed to take that as Integer.
Code:
a=str(input("Enter A Value \n"))
In the above code, it converts my input always as String. Because I used str there.
If I remove str from there and if I type some numbers in the input will it take as an integer?
Python 3.x will always consider input as string you have to type cast each field manually.
int(input('Enter Number'))
str(input('Enter String'))

Checking if input is integer in Python3.7 gives error [duplicate]

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.

In Python 2.x, is there anyway to check if a string can be converted to an integer? [duplicate]

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

Categories