Python Error Handling Without GOTO [duplicate] - python

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

Related

how to repetedly ask for input until a valid input is entered? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
i'm absolutley garbage at python 3. this may not be a hard question but ive tried for hours to create a solution to this question and cant make a functioning code. i've used while and if statements.
"As the user enters passenger details such as e-ticket number, full name, and destination, the program validates the user input. If an invalid input is entered,the program displays an appropriate error message and asks the user to re-enter invalid values until each entry is correct"
this is very easy
helle.py
while True:
inputdata = input()
if inputdata != "valid":
print("not valid")
else:
print("valid")
break

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

How to make a question, with an integer in it, asked again when it is left blank [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I am fairly new to python and I am not sure how to make a question involving an integer asked again when an invalid character is entered or it is left blank. Could someone please help me.
I have only tried what I know and nothing has really worked.
# Asks for age
age = int(input("Please enter your age: "))
# Prevents age from being left blank
# I need help with this part
while True:
try:
age = int(input("Please enter your age: "))
except ValueError:
continue
break
This runs in a loop until you get a valid value

How to re-prompt user input after incorrect type of data is given in Python 3.6 [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
This question has not been asked in relation to Python 3.6 .
I need a solution to re-prompt a specific user input question in a series of input questions if data is given as anything else but an integer or float in this case.
Lets say they input the correct float data for the first question, but then lets say that input a string character into the second question. That outputs a "ValueError: could not convert string to float: ".
Is there a way using looping or another method to re-prompt the SECOND input question that they failed to put integer/float data in? Furthermore, can you re-prompt only the second question instead of having to start over and re-prompt to the FIRST question?
counter = 0
counter += float(input("What is number 1?"))
counter += float(input("What is number 2?"))
counter += float(input("What is number 3?"))
print(counter)
EDIT: I did read the posted articles containing 9 answers which is similar, but non of them dealt with multiple input questions one after another. The provided answered were helpful, but I still don't quite get how to re-prompt the 2nd or 3rd question after an incorrect data type is entered. In summary: I would like the program to re-prompt the question that failed rather than having the user have to begin at question 1.
Something like this pseudo-code would work:
for q in questions:
while True:
ask_question
if question_result_validated:
break

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

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"

Categories