0

所以我正在制作我的简单计算器,这就是我到目前为止所拥有的:

import time
print ("Welcome. This is a calculator that uses the function: A (operator) B.")
time.sleep(3.5)
print ("Available operators include: Addition, Subtraction, Multiplication, Division,     Exponent, and Remainder division.")
time.sleep(3.5)
while True:
    a = float(input("Type in a value of A. "))
    b = float(input("Type in a value of B. "))
    opera = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
    opera = opera.lower()
    while not (opera) == "add" or (opera) == "subtract" or (opera) == "multiply" or (opera) == "divide" or (opera) == "exponent" or (opera) == "remainder":
        print ("Invalid operation.")
        opera = input("Would you like to: Add - Subtract - Multiply - Divide - Exponent - or Remainder? ")
        break
    if (opera) == "add":
        print ((a) + (b))
    if (opera) == "subtract":
        print ((a) - (b))
    if (opera) == "multiply":
        print ((a) * (b))
    if (opera) == "divide":
        print ((a) / (b))
    if (opera) == "exponent":
        print ((a) ** (b))
    if (opera) == "remainder":
        print ((a) % (b))
    cont = input("Would you like to do another problem?")
    cont = cont.lower()
    if cont != "yes":
        break
    quit

因此,当我启动计算器并输入 a 和 b 的值时,输入除 add 之外的任何内容都会导致无效操作。然后它会提示我进行有效操作,然后它适用于所有操作。我该如何解决?我假设问题与 while 不在 while true 内有关。

4

2 回答 2

1

我认为这是一个括号问题。

 while not (opera) == "add" or (opera) == "subtract" or (opera) == "multiply" or (opera) == "divide" or (opera) == "exponent" or (opera) == "remainder":

应该

 while not ((opera) == "add" or (opera) == "subtract" or (opera) == "multiply" or (opera) == "divide" or (opera) == "exponent" or (opera) == "remainder"):

您的 not 仅适用于第一个术语“添加”。在那之后它起作用的原因是你永远不会因为休息而回到没有条件的时候。

我将字典视为解决此类问题的一种更优雅的方法。

于 2013-11-10T14:19:04.293 回答
0

您的脚本实际上存在许多问题。请参阅我在下面所做的修改:

操作=('加','减','乘','除','指数','余数')

print ("Welcome. 这是一个使用函数的计算器:A (operator) B. \n") print ("Available operators include: {0}".format(operations))

while True: a = float(raw_input("键入 A 的值。")) b = float(raw_input("键入 B 的值"))

opera = raw_input("Which operation would you like to perform? ")
opera = opera.lower()

while opera not in operations:
    print ("Invalid operation.")
    opera = raw_input("Which operation would you like to perform? ")

if opera == "add":
    print (a + b)
if opera == "subtract":
    print (a - b)
if (opera) == "multiply":
    print (a * b)
if (opera) == "divide":
    print (a / b)
if (opera) == "exponent":
    print (a ** b)
if (opera) == "remainder":
    print (a % b)

if raw_input("Would you like to do another problem?").lower() != 'yes':
    break

问题 0:从用户的角度来看,睡眠语句总是很烦人,它在测试调试运行中增加了 7 秒。摆脱他们。如果你真的......真的......想要它们,完成后将它们添加回来。

问题 1:您使用了 input 函数而不是 raw_input 函数。阅读 python 文档,输入函数对您输入的任何内容执行 eval 语句。当我尝试执行“添加”命令时出现 NameError。请改用 raw_input。

问题 2:您的逻辑在 while 语句中是错误的。您可以说 while (opera != 'add) 或 ... 但我选择将所有有效值添加到元组中。这样更干净,更整洁。打破循环的逻辑也是错误的。无论您将跳出循环的输入是什么,您都可以使用 if 语句来获得相同的效果。

问题 3:这尖叫 c 风格的 switch 语句,所以使用字典。我会使用字典将操作的字符串形式(例如'add'、'multiply')绑定到函数ponters。它会更清晰,更pythonic。如果需要,我可以发布使用字典的代码。

于 2013-11-10T14:43:58.943 回答