你好我是python的新手,我正在尝试做一个任务,但我无法获得所需的输出我应该 得到。任何人都可以告诉我我缺少的东西吗?谢谢!
分配: 本章的最后一个练习将继续上一章的练习,即计算器。在本练习中,通过实现以下新功能来扩展现有代码:(A)计算器在给出结果时不会自动退出,允许用户进行新的计算。用户必须在菜单中选择“6”才能退出程序。 (B)计算器通过打印“当前数字:”和用户给定的输入在主菜单中显示所选数字。通过在计算器菜单中选择“5”,用户可以更改给定的数字。
如果正确实施,程序将打印出以下内容:
再次,在一个大的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:
答案 0 :(得分:0)
您需要使用标志(例如promptForNumbers
)来确定是否在循环中再次提示值。在循环开始之前将标志设置为True
,如果标志为value1
,则仅执行输入value2
和True
的值捕获。捕获数字后,将标记设置为False
。然后,仅在选择选项5时将标志设置为True
(更改数字)。
其他注意事项:
continue
语句也是多余的,因为if/elsif/elsif/../else
语句后面没有任何内容。selrction={1,2,3,4,5,6,}
语句是多余的(并且拼写错误),因为下一个选择分配是另一个分配,并且中间没有用。selection+=1
也是多余的,因为它没有被使用。int(input(value))
,而不是浮动float(input(value))
,将限制计算器的准确性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.")