The SyntaxError in the second example comes from the fact, that else needs no condition. The first example is totally ok.
Still better, keep the try-block as short as possible:
print("What is 1 + 1?")
while True:
try:
UserInput = int(input(("Your answer here:"))
except ValueError:
print("That is not a number. Try again!")
else:
if UserInput == 2:
print("Congratulations you are correct!")
break
else:
print("That is incorrect. Try again!")
Answer from Daniel on Stack OverflowExplain Python ValueError Exception Handling (With Real Examples & Best Practices)
Python: else ValueError: (Specifically ValueError In This Case) - Stack Overflow
python - python3 ValueError value - Stack Overflow
Python: if error raised I want to stay in script - Stack Overflow
Videos
The SyntaxError in the second example comes from the fact, that else needs no condition. The first example is totally ok.
Still better, keep the try-block as short as possible:
print("What is 1 + 1?")
while True:
try:
UserInput = int(input(("Your answer here:"))
except ValueError:
print("That is not a number. Try again!")
else:
if UserInput == 2:
print("Congratulations you are correct!")
break
else:
print("That is incorrect. Try again!")
try and except are a form of control flow. Essentially, it means try to run this code, except if an exception occurs (such as ValueError) do something else.
if and else are another form of control flow. Together, they mean if a condition is true, do something; else, do something else.
An exception occurring is not a condition, and thus it would not make sense to use else with an exception like ValueError. Instead, you want to use the try/except block.
Use try/except.
>>> while True:
... try:
... x = int(raw_input("Please enter a number: "))
... break
... except ValueError:
... print "Oops! That was no valid number. Try again..."
...
success = false
while not success:
try:
value = raw_input('please enter an integer')
int(value)
success = true
except:
pass
You should use try...except ValueError, as a ValueError is raised when integer cannot convert something to an integer
choice = [1, 2, 3]
def configselect(choice):
if choice == 1:
print("you chose 962")
elif choice == 2:
print("you chose Audience")
elif choice == 3:
print("you chose mAP")
while True:
try:
val=int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? "))
if val in choice:
configselect(val)
break
else:
print("Please select either 1,2 or 3")
except ValueError:
print('Thats not a valid Number. Please try Again.')
If you want to catch the exception, you need to add a try-except block:
try:
configselect(int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? ")))
except ValueError:
print("Sorry, that's not right, try again")
If you want to prompt for input continuously until the user enters something acceptable, you need to wrap this in a loop:
while True:
try:
configselect(int(input("Are you configuring a (1)962, (2)Audience, or (3)mAP? ")))
except ValueError:
print("Sorry, that's not right, try again")
else:
break