Python Divide by 0 Error

我是研究僧i 提交于 2019-12-02 13:36:04

You can except the ZeroDivisionError like this

x = "1/0"
try:
    exec(x)
except ZeroDivisionError:
    print ("WARNING: Invalid Equation")

If you are using Python 2.x, the input data will be evaluated here itself

y = input(" >> ")

so, in Python 2.x, you should be using

y = raw_input(" >> ")

Apart from that, you can improve this piece of code

if False in [c in valid_chars for c in y]:

like this

valid_chars = set("0123456789-+/* \n")    # We make this a set, because
if not all(c in valid_chars for c in y):  # c in valid_chars will be in O(1)

As @gnibbler, suggested in the comments section, the same if condition can be written like this

if any(c not in valid_chars for c in y):  # c in valid_chars will be in O(1)

Another suggestion would be, no need to use semi colons to mark the end of line in Python :)

encapsulate your code with a try except

try:
  y = input(" >> ")
except Exception as exc:
  # process exception here

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: .

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 run exec(64). Look more carefully at the logic flow around try: ... except: ... else.

The simplest fix is to start with

x = "z="

and then print z rather than x. This should not stop you working to avoid the duplicated execution of your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!