How can you get python to detect strings from integers [duplicate] - python

This question already has answers here:
How can I check if string input is a number?
(30 answers)
Closed 7 years ago.
In my program I need a hand in trying to get python to say that a input is invalid, the code asks for numbers, if you input a letter I would like it to say its invalid rather than breaking, is there any way I can do this?

Here is some code that works using a try/except block. The code attempts to convert the input into a number, if it succeeds it breaks out of the loop, if not the error is caught and an error message is printed, and then the loop starts again
passed = False
print "input a number"
while not passed:
a = raw_input()
try:
number = int(a)
passed = True
print "Number inputted"
except(ValueError):
print "You did not input a number, try again"

Related

TypeError in if else statement [duplicate]

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

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.

Python Error Handling Without GOTO [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I am currently getting user input with this line of code:
qty = int(input("How many of this item should we start with?"))
As one would expect, it throws an error if a value is entered that cannot be converted to INT.
On Error, I would like to prompt "Please enter a whole number" and return to the previous line requesting input. What is the most "Pythonic" way of achieving this?
Probably, the most pythonic way is to do the following
while True:
try:
qty = int(input("How many of this item should we start with?"))
break
except ValueError:
pass

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

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.

Stop input function in python [duplicate]

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 8 years ago.
I would like to stop the user to type in too many characters in the input function so it just stops the input() when too many characters are put in but the characters previously typed in stay. In this case I wouln't want to check if this is after the user has pressed enter but I would like to interrupt the function. Is this somehow possible?
Might be possible with a loop and msvcrt. Here is an example:
while len(string) < 10:
#Whatever length you want instead of 10
string += msvcrt.getch()
I don't believe this is possible, but if you're concerned about a maximum length (for a database value or something similar) you can use slicing to address excessively long entries:
maxlen = 256
userval = input("Enter value (max 256):")[:maxlen]

Categories