Python中if语句未执行:简易计算器代码故障排查
Python简易计算器分支不执行的问题修复
你的代码存在三个核心问题,导致分支无法触发或后续运算报错:
1. 功能选择的类型不匹配
userInput是input()返回的字符串,后续用userInput == 1这类整数比较永远不会成立,自然无法进入分支。需要在验证通过后,将userInput转为整数并赋值:
# 原代码此处为无效的str(userInput),替换为: userInput = int(userInput) break
2. 数值输入未实际转换类型
输入variableA和variableB时,float(variableA)仅执行了转换操作,但未将结果存回变量,导致变量仍为字符串类型,传给运算函数会触发错误。修改为:
# 原代码此处为float(variableA),替换为: variableA = float(variableA) break
variableB做同样修改即可。
3. 第6分支的逻辑错误
你误用sub函数判断数值大小,应该调用自己定义的gt函数;且原逻辑中sub返回数值,和True/False比较完全不成立。修正第6分支代码:
elif userInput == 6: print("Is the first input greater than the second?") result = gt(variableA, variableB) if result == "Equal": print(f"It is {result}. Both numbers are the same.") elif result: print(f"{result}. {variableA} is greater than {variableB}") else: print(f"{result}. {variableB} is greater than {variableA}") print("Thank you")
修改后的完整代码
import operator as op print("Greetings user, welcome to the calculator program.\nWe offer a list of functions:") print("1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Modulus\n6. Check greater number") while True: userInput = input("Please choose what function you would like to use based on their numbers:") if userInput.isdigit(): if int(userInput) in range(1,7): userInput = int(userInput) break else: print("Number inputted is either below or above the given choices") continue else: print("Incorrect input. Please try again.") continue def add(x,y): return op.add(x,y) def sub(x,y): return op.sub(x,y) def mul(x,y): return op.mul(x,y) def div(x,y): return op.truediv(x,y) def mod(x,y): return op.mod(x,y) def gt(x,y): if x == y: return "Equal" else: return op.gt(x,y) variableA = 0 variableB = 0 while True: variableA = input("Enter the first value: ") if variableA.isdigit(): variableA = float(variableA) break else: print("Incorrect input. Please try again.") continue while True: variableB = input("Enter the second value: ") if variableB.isdigit(): variableB = float(variableB) break else: print("Incorrect input. Please try again.") continue if userInput == 1: print("You chose to add the two numbers and the result is:") print(add(variableA,variableB)) print("Thank you") elif userInput == 2: print("You chose to subtract with the two numbers and the result is:") print(sub(variableA,variableB)) print("Thank you") elif userInput == 3: print("You chose to multiply the two numbers and the result is:") print(mul(variableA,variableB)) print("Thank you") elif userInput == 4: print("You chose to divide with the two numbers and the result is:") print(div(variableA,variableB)) print("Thank you") elif userInput == 5: print("You chose to find the modulo with the two numbers and the result is:") print(mod(variableA,variableB)) print("Thank you") elif userInput == 6: print("Is the first input greater than the second?") result = gt(variableA, variableB) if result == "Equal": print(f"It is {result}. Both numbers are the same.") elif result: print(f"{result}. {variableA} is greater than {variableB}") else: print(f"{result}. {variableB} is greater than {variableA}") print("Thank you")
内容的提问来源于stack exchange,提问作者Depleted Money
相关产品推荐
相关产品推荐

