Try/except 在 Python - 在 range/eof 之外
Try/except in Python - out of range/eof
我是 Python 的初学者。我刚刚在Python中遇到了try/except。当我使用 except 时,如果没有 Eof 问题或超出范围,我很难处理这些事情。
我想遍历元组列表以检查有多少学生通过了考试,有多少学生未通过考试。不幸的是,并非所有学生都有 pass/will 子句。
如何在不出现运行时错误的情况下解决这个问题?
try:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
try:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
except IndexError:
for tup in students:
print(tup[2])
# ...
首先,read the docs - lots of examples in there, see also IndexError.
访问无效索引时如何不崩溃。
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
try:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
except IndexError:
# handle error here
pass
另一个没有 try/except 的解决方案 - 只需检查元组的 len()
:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
counter = [0, 0, 0]
for student in students:
if len(student) > 2 and student[2] == "Will pass":
counter[0] += 1
elif len(student) > 2 and student[2] == "Will not pass":
counter[1] += 1
else:
counter[2] += 1
print("Passing:", counter[0], "Not passing:", counter[1], "Missing attribute:", counter[2])
这是另一个使用 filter
、map
和 reduce
的解决方案,只是为了好玩:
from functools import reduce
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
p, n = reduce(
lambda x, y: (x[0] + y[0], x[1] + y[1]),
map(
lambda x: (1,0) if x[2] == 'Will pass' else (0,1),
filter(
lambda x: len(x) > 2,
students
)
),
(0,0)
)
print("Passing:", p, "Not passing:", n)
我看你想练习try/except。在那种情况下你应该记住:
- 每个 try 块必须有一个 except 块,否则会抛出错误
- 您只需要将代码块放在可能发生错误的 try 块中。
因此您可以对代码进行以下修改:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
try:
passing[tup[2]] += 1
except:
print('No status for student: ', tup[0])
print(passing)
这会产生以下输出:
No status for student: Martha
No status for student: Ashley
No status for student: Archie
{'Will pass': 3, 'Will not pass': 2}
我是 Python 的初学者。我刚刚在Python中遇到了try/except。当我使用 except 时,如果没有 Eof 问题或超出范围,我很难处理这些事情。
我想遍历元组列表以检查有多少学生通过了考试,有多少学生未通过考试。不幸的是,并非所有学生都有 pass/will 子句。
如何在不出现运行时错误的情况下解决这个问题?
try:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
try:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
except IndexError:
for tup in students:
print(tup[2])
# ...
首先,read the docs - lots of examples in there, see also IndexError.
访问无效索引时如何不崩溃。
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
try:
if tup[2] == "Will pass":
passing['Will pass'] += 1
elif tup[2] == "Will not pass":
passing["Will not pass"] += 1
except IndexError:
# handle error here
pass
另一个没有 try/except 的解决方案 - 只需检查元组的 len()
:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
counter = [0, 0, 0]
for student in students:
if len(student) > 2 and student[2] == "Will pass":
counter[0] += 1
elif len(student) > 2 and student[2] == "Will not pass":
counter[1] += 1
else:
counter[2] += 1
print("Passing:", counter[0], "Not passing:", counter[1], "Missing attribute:", counter[2])
这是另一个使用 filter
、map
和 reduce
的解决方案,只是为了好玩:
from functools import reduce
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'),
('Archie', 71), ('Carl', 45, 'Will not pass')]
p, n = reduce(
lambda x, y: (x[0] + y[0], x[1] + y[1]),
map(
lambda x: (1,0) if x[2] == 'Will pass' else (0,1),
filter(
lambda x: len(x) > 2,
students
)
),
(0,0)
)
print("Passing:", p, "Not passing:", n)
我看你想练习try/except。在那种情况下你应该记住:
- 每个 try 块必须有一个 except 块,否则会抛出错误
- 您只需要将代码块放在可能发生错误的 try 块中。
因此您可以对代码进行以下修改:
students = [('Timmy', 95, 'Will pass'), ('Martha', 70), ('Betty', 82, 'Will pass'),
('Stewart', 50, 'Will not pass'), ('Ashley', 68), ('Natalie', 99, 'Will pass'), ('Archie', 71),
('Carl', 45, 'Will not pass')]
passing = {'Will pass': 0, 'Will not pass': 0}
for tup in students:
try:
passing[tup[2]] += 1
except:
print('No status for student: ', tup[0])
print(passing)
这会产生以下输出:
No status for student: Martha
No status for student: Ashley
No status for student: Archie
{'Will pass': 3, 'Will not pass': 2}