I am currently using this Python code:
valid_chars = "0123456789-+/* \n";
while True:
x = "x="
y = input(" >> ")
x += y
if False in [c in valid_chars for c in y]:
print("WARNING: Invalid Equation");
continue;
if(y == "end"):
break
exec(x)
print(x)
It crashes when the user does something like this: 9/0. Error:
ZeroDivisionError: division by zero
What are some ways that will prevent the user from dividing something by zero?
You can except the
ZeroDivisionError
like thisIf you are using Python 2.x, the input data will be evaluated here itself
so, in Python 2.x, you should be using
Apart from that, you can improve this piece of code
like this
As @gnibbler, suggested in the comments section, the same if condition can be written like this
Another suggestion would be, no need to use semi colons to mark the end of line in Python :)
The real problem is that you are executing
exec()
twice. The first time you execute it on the string you have constructed, and this replaces the value of x with the result of your entered expression. So the second time you are presumably trying to runexec(64)
. Look more carefully at the logic flow aroundtry: ... except: ... else
.The simplest fix is to start with
and then print
z
rather thanx
. This should not stop you working to avoid the duplicated execution of your code.encapsulate your code with a try except
The reason is the input() is being evaluated as it is read in before y is set. Thus the exception occurs because of the input() and must be caught with the try: .