【发布时间】:2021-11-20 01:49:09
【问题描述】:
假设我想检查 2 个考试成绩的平均值。我不希望用户输入任何字母或小于 0 的值。
我想到的是这段代码:
while True:
try:
score1 = float(input('Enter the your 1st score: '))
score2 = float(input('Enter the your 2nd score: '))
except:
print('Invalid value, try again...')
continue
if (score1 < 0 or score2 < 0):
print('Invalid value, try again...')
continue
print(f'Your average is {(score1 + score2) / 2}')
break
有没有办法在同一个 if 语句中检查异常以及分数是否小于 0?
【问题讨论】:
-
欢迎来到 Stack Overflow。如果您尝试了该代码并且它有效,那么这就是我们可以为您做的所有事情。这不是讨论论坛、辅导服务或代码质量研讨会。
标签: python if-statement exception