【问题标题】:print statement outside except block除了块之外的打印语句
【发布时间】:2020-08-17 13:32:23
【问题描述】:
import sys
lst = ['b', 0, 2,5]
for entry in lst:
  try:
    print("****************************")
    print("The entry is", entry)
    r = 1 / int(entry)
  except(ValueError):
    print("This is a ValueError.")
  except(ZeroDivisionError):
    print("This is a ZeroError.")
  except:
    print("Some other error")
print("The reciprocal of", entry, "is", r)

当程序将输入作为 2 时,除了块之外的打印语句将被跳过。但如果我只在列表中给出 3 个元素,它就可以正常工作。

【问题讨论】:

    标签: python try-except


    【解决方案1】:

    这是因为 print 语句在 for 循环之外。所以它只打印entry 的最后一次迭代。

    检查一下:

    In [1340]: import sys 
          ...: lst = ['b'] 
          ...: for entry in lst: 
          ...:   try: 
          ...:     print("****************************") 
          ...:     print("The entry is", entry) 
          ...:     r = 1 / int(entry) 
          ...:   except(ValueError): 
          ...:     print("This is a ValueError.") 
          ...:   except(ZeroDivisionError): 
          ...:     print("This is a ZeroError.") 
          ...:   except: 
          ...:     print("Some other error") 
          ...: print("The reciprocal of", entry, "is", r)
    ****************************
    The entry is b
    This is a ValueError.
    The reciprocal of b is 0.2
    

    如果您希望它为每个元素打印,请将打印推入 for 循环:

    In [1344]: import sys 
          ...: lst = ['b',0,2,5] 
          ...: for entry in lst: 
          ...:   try: 
          ...:     print("****************************") 
          ...:     print("The entry is", entry) 
          ...:     r = 1 / int(entry) 
          ...:   except(ValueError): 
          ...:     print("This is a ValueError.") 
          ...:   except(ZeroDivisionError): 
          ...:     print("This is a ZeroError.") 
          ...:   except: 
          ...:     print("Some other error") 
          ...:   else: 
          ...:     print("The reciprocal of", entry, "is", r) 
          ...:                                                                                                                                                                                                  
    ****************************
    The entry is b
    This is a ValueError.
    ****************************
    The entry is 0
    This is a ZeroError.
    ****************************
    The entry is 2
    The reciprocal of 2 is 0.5
    ****************************
    The entry is 5
    The reciprocal of 5 is 0.2
    

    【讨论】:

      【解决方案2】:

      由于您的print 位于for 循环之外,因此它仅在循环的最后一次迭代之后执行,即5 值。您需要将其放入循环中,但将其放入 tryelse 子句中,以便仅在没有发生异常时执行:

      import sys
      lst = ['b', 0, 2,5]
      for entry in lst:
        try:
          print("****************************")
          print("The entry is", entry)
          r = 1 / int(entry)
        except(ValueError):
          print("This is a ValueError.")
        except(ZeroDivisionError):
          print("This is a ZeroError.")
        except:
          print("Some other error")
        else:
          print("The reciprocal of", entry, "is", r)
      

      输出:

      ****************************
      The entry is b
      This is a ValueError.
      ****************************
      The entry is 0
      This is a ZeroError.
      ****************************
      The entry is 2
      The reciprocal of 2 is 0.5
      ****************************
      The entry is 5
      The reciprocal of 5 is 0.2
      

      【讨论】:

        【解决方案3】:
        lst = ['b', 0, 2, 5]
        for entry in lst:
          try:
            print("****************************")
            print("The entry is", entry)
            r = 1 / int(entry)
            print("The reciprocal of", entry, "is", r)
          except(ValueError):
            print("This is a ValueError.")
          except(ZeroDivisionError):
            print("This is a ZeroError.")
          except:
            print("Some other error")
        

        print 语句应该在 try 块内

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多