Это потому, что оператор print
находится за пределами для l oop. Таким образом, он печатает только для последней итерации 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
Если вы хотите, чтобы он печатал для каждого элемента, pu sh печать внутри для л oop:
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