You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python计算器重置功能添加、select_op使用及菜单重显问题

Python计算器三个问题修复方案

针对你提出的三个问题,修复逻辑如下:

  • 重置功能实现:在所有输入节点(运算符选择、第一个数字输入、第二个数字输入)增加$标识检测,一旦捕获到输入为$,立刻通过continue关键字跳过当前循环剩余逻辑,回到主循环起始位置完成重置;数字输入环节增加异常捕获,避免非数字输入触发float()转换报错,同时给用户明确的输入错误提示。
  • select_op函数正确使用:拿到用户输入的操作选项后,仅调用一次select_op,将返回的操作编码存入固定变量,后续分支判断直接读取该变量即可,避免重复调用函数产生不必要的性能开销;同时修正原代码中if/elif缩进不匹配的语法错误。你需要提前在select_op中实现选项映射:支持用户输入数字编号(1-8)和对应运算符(+/-/*/等)两种输入格式,匹配到对应操作后返回统一数字编码,匹配失败返回-1代表非法输入。
  • 操作后自动重打菜单:将功能菜单的打印代码全部放在while True主循环的最内侧开头位置,只要不触发终止退出逻辑,每轮循环启动时都会自动打印菜单,无需重复编写打印代码。

额外修复原代码的基础语法问题:原代码while循环下属代码未做缩进,会直接触发语法报错;原逻辑执行完一轮操作后会直接走到循环外的退出代码,需要调整逻辑:仅当用户选择终止操作时,才通过break跳出主循环执行退出提示。

修复后完整可运行代码

# 实现基础运算函数与select_op映射函数
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

def multiply(a, b):
    return a * b

def divide(a, b):
    if b == 0:
        return "Error! Cannot divide by zero"
    return a / b

def power(a, b):
    return a ** b

def remainder(a, b):
    if b == 0:
        return "Error! Cannot take remainder with zero divisor"
    return a % b

def select_op(choice):
    # 同时支持数字选项和运算符输入
    op_map = {
        '1': 1, '+': 1,
        '2': 2, '-': 2,
        '3': 3, '*': 3,
        '4': 4, '/': 4,
        '5': 5, '^': 5,
        '6': 6, '%': 6,
        '7': 7, '#': 7,
        '8': 8, '$': 8
    }
    return op_map.get(choice, -1)

# 主程序循环
while True:
    # 菜单放在循环开头,每次循环自动打印
    print("Select operation.")
    print("1.Add      : + ")
    print("2.Subtract : - ")
    print("3.Multiply : * ")
    print("4.Divide   : / ")
    print("5.Power    : ^ ")
    print("6.Remainder: % ")
    print("7.Terminate: # ")
    print("8.Reset    : $ ")

    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
    print(choice)
    op_code = select_op(choice)

    # 优先处理重置、终止、非法输入逻辑
    if op_code == 8: # 触发重置
        continue
    if op_code == 7: # 触发终止
        break
    if op_code == -1: # 非法运算符
        print("Unrecognized operation")
        continue

    # 获取第一个数字,支持输入$重置
    reset = False
    while True:
        num1_input = input("Enter first number (input $ to reset): ")
        if num1_input == '$':
            reset = True
            break
        try:
            num1 = float(num1_input)
            break
        except ValueError:
            print("Invalid number, please re-enter or input $ to reset")
    if reset:
        continue

    # 获取第二个数字,支持输入$重置
    while True:
        num2_input = input("Enter second number (input $ to reset): ")
        if num2_input == '$':
            reset = True
            break
        try:
            num2 = float(num2_input)
            break
        except ValueError:
            print("Invalid number, please re-enter or input $ to reset")
    if reset:
        continue

    # 运算分支判断,直接使用预存的op_code,无需重复调用select_op
    if op_code == 1:
        print(num1 , " + " , num2 , " = " , add(num1,num2))
    elif op_code == 2:
        print(num1 , " - " , num2 , " = " , subtract(num1,num2))  
    elif op_code == 3:
        print(num1 , " * " , num2 , " = " , multiply(num1,num2))
    elif op_code == 4:
        print(num1 , " / " , num2 , " = " , divide(num1,num2))
    elif op_code == 5:
        print(num1 , " ^ " , num2 , " = " , power(num1,num2))
    elif op_code == 6:
        print(num1 , " % " , num2 , " = " , remainder(num1,num2))

# 跳出循环后执行终止逻辑
print("Done. Terminating")
exit()

内容的提问来源于stack exchange,提问作者Pathum Sandeepa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.04 16:15:42