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

Python基础计算器添加for循环实现重复计算的位置咨询

实现计算器重复运行的方案

其实while循环比for循环更适合这个场景——因为我们没法提前知道用户要进行多少次计算,用无限循环+退出选项会更灵活。不过如果你一定要用for循环,可以设定固定的循环次数,下面分别说明:

方式1:用while循环(推荐)

把整个计算流程(从输入操作到输出结果的所有代码)包裹在while True循环里,同时新增退出选项让用户能主动结束程序。修改后的代码如下:

operation_list = ['+', '-', 'x', '/', '**', 'root', 'quit']

while True:
    op = input(f"{operation_list}\nChoose an Operation:")
    
    if op == "quit":
        print("计算器已退出")
        break
    elif op == "**":
        num4= int(input("Enter the number you want to exponent:"))
        num3 = int(input("Enter exponent:"))
        print(num4, "**", num3, "=", num4 ** num3)
        
    elif op == "root":
        num5= int(input("Enter the Number You Want to Root:"))
        print(num5," root", "=", num5 ** 0.5)

    elif op == "+":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "+", num2, "=", num1 + num2)
        
    elif op == "-":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "-", num2, "=", num1 - num2)
        
    elif op == "x":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "x", num2, "=", num1 * num2)
        
    elif op == "/":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "/", num2, "=", num1 / num2)
        
    else:
        print("There is no such command. Please choose one of the following commands (+, -, x, /, **, root, quit).")

关键说明:

  • 所有交互逻辑(输入操作、计算、输出)都被包裹在while True块内,每次计算完成后会自动回到开头,让用户选择新操作。
  • 新增的quit选项允许用户主动终止循环,结束程序运行。

方式2:用for循环(固定次数)

如果你一定要用for循环,可以先让用户输入计算次数,然后循环对应次数:

operation_list = ['+', '-', 'x', '/', '**', 'root']

# 先让用户输入要计算的次数
count = int(input("请输入要计算的次数:"))

for _ in range(count):
    op = input(f"{operation_list}\nChoose an Operation:")
    
    if op == "**":
        num4= int(input("Enter the number you want to exponent:"))
        num3 = int(input("Enter exponent:"))
        print(num4, "**", num3, "=", num4 ** num3)
        
    elif op == "root":
        num5= int(input("Enter the Number You Want to Root:"))
        print(num5," root", "=", num5 ** 0.5)

    elif op == "+":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "+", num2, "=", num1 + num2)
        
    elif op == "-":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "-", num2, "=", num1 - num2)
        
    elif op == "x":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "x", num2, "=", num1 * num2)
        
    elif op == "/":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "/", num2, "=", num1 / num2)
        
    else:
        print("There is no such command. Please choose one of the following commands (+, -, x, /, **, root).")

关键说明:

  • 用range(count)生成指定次数的循环,每次循环执行一次完整的计算流程,循环结束后程序自动退出。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:40:23