Python Divide by 0 Error

前端 未结 3 1861
别那么骄傲
别那么骄傲 2021-01-29 13:34

I am currently using this Python code:

valid_chars = \"0123456789-+/* \\n\";
while True:
    x = \"x=\"
    y = input(\" >> \")
    x += y
    if False in          


        
3条回答
  •  后悔当初
    2021-01-29 14:14

    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.

提交回复
热议问题