Python Divide by 0 Error

前端 未结 3 1867
别那么骄傲
别那么骄傲 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:09

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

提交回复
热议问题