Reputation: 19
What I am planning is when the user inputs something that results to an error (in my code, is the eval() which will cause the error in the input), it loops back.
while True:
x = input('Enter price: ')
y = eval(x)
if y == #error condition
print('Error! Please try again.')
continue
else:
print(y)
break
The output should be:
Enter price: +
Error! Please try again.
Enter price: 1+
Error! Please try again.
Enter price: 1+1
2
Is it possible? I am using Python 3.4 btw.
Upvotes: 1
Views: 1531
Reputation: 1803
You can use the try ... except
construct. It will catch Exceptions:
while True:
x = input('Enter price: ')
try:
y = eval(x)
except Exception:
print('Error! Please try again.')
continue
print(y)
break
Upvotes: 2