Python学分校验程序故障求助:始终输出‘Out of range’问题排查
问题描述
程序要求:当输入的学分不在0、20、40、60、80、100、120范围内时,显示‘Out of range’。
我创建了包含合法学分值的列表,再将输入的三个学分变量存入另一个列表,尝试用if语句判断变量是否在合法列表中,但程序始终输出‘Out of range...’;改用while循环时,会重复输出总和7次。不知道怎么修复,求解答。
原代码
def main(): a = [0, 20, 40, 60, 80, 100, 120] try: userPass = int(input("Enter pass credits: ")) userDefer = int(input("Enter defer credits: ")) userFail = int(input("Enter fail credits: ")) except: print("Invalid number.") total = userFail + userPass + userDefer aValues = [ userPass, userFail, userDefer ] if aValues in a: print(total) else: print("Out of range. Try again") if total > 120: print("Total incorrect") repeat() else: if userPass >= 120: print("This student has progressed.") repeat() elif userPass >= 100 and total == 120: print("This student is trailing.") repeat() elif userFail <= 60 and total == 120: print("This student did not progress(module retreiver).") repeat() elif userPass <= userFail: print("This students program outcome is exclude.") repeat() else: print("incorrect. Try again.") repeat() def repeat(): choice = int(input("If you would like to quit type 1 or test another student write 2: ")) if choice != 1 and choice != 2: repeat() else: while choice == 2: main() while choice == 1: break print("Thank you.") main()
问题分析与修复方案
1. 核心错误:范围判断逻辑完全错误
你写的if aValues in a:是在判断整个三元素列表是否是合法列表a中的元素,但a里都是单个数值,这永远不会成立,所以才会一直输出"Out of range"。
正确逻辑是检查每个输入的学分是否都在合法列表中,可以用all()函数简化判断:
# 替换原判断代码 valid = all(credit in a for credit in aValues) if valid: print(total) else: print("Out of range. Try again")
2. 异常处理后的变量未定义问题
如果输入非整数触发except块,userPass、userDefer、userFail会未定义,后续代码引用会直接报错。需要在异常处理后终止当前main()执行:
try: userPass = int(input("Enter pass credits: ")) userDefer = int(input("Enter defer credits: ")) userFail = int(input("Enter fail credits: ")) except: print("Invalid number.") return # 直接返回,避免后续报错
3. repeat()函数循环逻辑错误
当选择2时,while choice == 2:会无限调用main(),因为choice值不会更新,导致重复执行。改成单次调用后重新进入选择流程即可:
def repeat(): choice = int(input("If you would like to quit type 1 or test another student write 2: ")) if choice == 1: print("Thank you.") elif choice == 2: main() else: print("Invalid choice, try again.") repeat()
4. 其他优化点
- 合法学分用集合存储,查询速度更快:
VALID_CREDITS = {0,20,40,60,80,100,120} - 修复拼写错误:
module retreiver改为module retriever,students改为student's - 把范围检查放在总和计算之前,避免无效运算
修复后的完整代码
VALID_CREDITS = {0, 20, 40, 60, 80, 100, 120} def main(): try: userPass = int(input("Enter pass credits: ")) userDefer = int(input("Enter defer credits: ")) userFail = int(input("Enter fail credits: ")) except ValueError: print("Invalid number.") repeat() return # 检查每个学分是否合法 if not all(credit in VALID_CREDITS for credit in [userPass, userDefer, userFail]): print("Out of range. Try again") repeat() return total = userPass + userDefer + userFail if total > 120: print("Total incorrect") repeat() return elif total != 120: print("Total must be 120. Try again.") repeat() return # 判断学生进度 if userPass >= 120: print("This student has progressed.") elif userPass >= 100: print("This student is trailing.") elif userFail <= 60: print("This student did not progress(module retriever).") elif userPass <= userFail: print("This student's program outcome is excluded.") else: print("Incorrect input. Try again.") repeat() def repeat(): try: choice = int(input("If you would like to quit type 1 or test another student write 2: ")) except ValueError: print("Invalid input, please enter 1 or 2.") repeat() return if choice == 1: print("Thank you.") elif choice == 2: main() else: print("Invalid choice, try again.") repeat() main()
内容的提问来源于stack exchange,提问作者A C
相关产品推荐
相关产品推荐

