Python计算器:多种选项,改变数字

2 投票
1 回答
2016 浏览
提问于 2025-04-17 20:44

你好,我是Python的新手,我正在做一个作业,但我没有得到我应该得到的结果。有人能告诉我我缺少了什么吗?谢谢!

作业: 这一章的最后一个练习是继续上章的计算器练习。在这个练习中,要在现有代码的基础上增加以下新功能:(A) 计算器在给出结果后不会自动退出,用户可以继续进行新的计算。用户必须在菜单中选择“6”来退出程序。(B) 计算器在主菜单中显示当前选择的数字,打印“当前数字:”以及用户输入的内容。用户可以通过选择计算器菜单中的“5”来更改输入的数字。

如果正确实现,程序将输出以下内容:

再次强调,程序应该放在一个大的 while True 循环中,只有当用户选择“6”选项时才会用 break 结束这个循环。

示例输出

Calculator
Give the first number: 100
Give the second number: 25
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 100 25
Please select something (1-6): 5
Give the first number: 10
Give the second number: 30
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 1
The result is: 40
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 6
Thank you! 

我的代码:

print("Calculator")  
    while True:  
        selrction={1,2,3,4,5,6,}  
        value1 = int(input("Give the first number: "))  
        value2 = int(input("Give the second number: "))  
        print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")  
        print("Current numbers: ",value1,value2)  
        selection=int(input("Please select something (1-6): "))  


        if selection==1:  
            print("The result is: ",(value1+value2))  
        elif selection==2:  
            print("The result is: ",(value1-value2))  
        elif selection==3:  
            print("The result is: ", (value1*value2))  
        elif selection==4:  
            print("The result is: ",(value1/value2))  
        elif selection==6:  
            print("Thank you!")  
            break  
        elif selection==5:  
            print("Change numbers")  
            continue  
        else:  
            print("Selection was not correct.")  
            selection+=1

我的输出:

Calculator
Give the first number: 100
Give the second number: 25
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 100 25
Please select something (1-6): 5
Change numbers
Give the first number: 10
Give the second number: 30
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 1
The result is: 40
Give the first number: 6
Give the second number:

1 个回答

0

你需要使用一个标志(比如说 promptForNumbers)来判断在循环中是否需要再次提示输入值。在循环开始之前,把这个标志设置为 True,只有当标志为 True 时,才会去获取输入的值 value1value2。一旦获取了这两个数字,就把标志设置为 False。然后,只有在选择了选项5(更改数字)时,才把标志重新设置为 True

还有一些其他需要注意的点:

  • continue 语句是多余的,因为在 if/elsif/elsif/../else 语句后面没有其他代码。
  • selrction={1,2,3,4,5,6,} 这行代码也是多余的(而且拼写错误),因为接下来对 selection 的赋值又是另一个赋值,中间没有用到。
  • selection+=1 也是多余的,因为没有用到这个值。
  • 将输入转换为整数 int(input(value)) 而不是浮点数 float(input(value)),会限制计算器的精度。
  • 调整了缩进,使其符合预期的 Python 格式。
  • 没有处理除以零的错误(所以如果第二个数字输入为 0,然后选择除法 /,程序会因为异常而退出)。

下面是根据上述描述修改后的代码:

print("Calculator")

# Flag to indicate whether user should be prompted for input numbers
promptForNumbers = True

while True:

    if promptForNumbers:
        value1 = float(input("Give the first number: "))
        value2 = float(input("Give the second number: "))
        # Set prompt flag false to prevent re-prompting for numbers upon next loop
        promptForNumbers = False

    print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")
    print "Current numbers: %s, %s" % (value1, value2)
    selection = int(input("Please select something (1-6): "))

    if selection == 1:
        print("The result is: %s" % (value1 + value2))
    elif selection == 2:
        print("The result is: %s" % (value1-value2))
    elif selection==3:
        print("The result is: %s" % (value1*value2))
    elif selection==4:
        print("The result is: %s" % (value1/value2))
    elif selection==6:
        print("Thank you!")
        break
    elif selection==5:
        # Set prompt flag so that numbers will be requested upon next loop
        promptForNumbers = True
    else:
        print("Selection was not correct.")

撰写回答