我试图阻止用户输入超过玩家池的团队成员数量。while True: try: numMembers = int(input("How many students per team (2, or 3) ")) if numMembers > totalStudents: errorHand = 1/0 except: print("Too many members per team, please try again") continue else: break只是想避免我想出的 hackity div/0 技巧。
1 回答
扬帆大鱼
TA贡献1799条经验 获得超9个赞
由于您需要在每个错误情况下做一些不同的事情,因此可以消除的复杂性并不多,但是您可以将 减少try到特定位置的特定异常并避免除以零 hack:
while True:
try:
numMembers = int(input("How many students per team (2, or 3) "))
except ValueError:
continue
if numMembers > totalStudents:
print("Too many members per team, please try again")
continue
break
添加回答
举报
0/150
提交
取消