1

我对 Python 还是很陌生,对我正在开发的程序发生了什么感到困惑。代码如下。问题是代码从不运行任何 if/elif/else 行。它只是不断循环菜单和输入。我正在使用 3.2。

# Program to Add, Subtract, Multiply, and Divide

def printmenu():
    print("Calculator v0.01")
    print("[A]dd Two Numbers")
    print("[S]ubtract Two Numbers")
    print("[M]ultiply Two Numbers")
    print("[D]ivide Two Numbers")
    print("[Q]uit the Program")

choice = "x"

while choice.lower != "q":
    printmenu()
    choice = input("What would you like to do? ")
    firstnum = input("What is the first number? ")
    secnum = input("What is the second number? ")
    if choice.lower == "a":
        print("The answer is ",  (firstnum + secnum))
    elif choice.lower == "s":
        print("The answer is ",  (firstnum - secnum))
    elif choice.lower == "m":
        print("The answer is ",  (firstnum * secnum))
    elif choice.lower == "d":
        print("The answer is ",  (firstnum / secnum))
    else:
        print("Choice not recognized.  Try again!")

PS - 这是我在这里的第一篇文章,所以如果我做的不好,请告诉我。

谢谢!

捷通

4

3 回答 3

3
>>> "a".lower
<built-in method lower of str object at 0x0000000001EBBBC0>
>>> "a".lower()
'a'
>>> "a".lower == "a"
False
>>> "a".lower() == "a"
True
>>>

我认为您的意思是较低(),而不是较低;)

于 2013-04-20T11:20:52.940 回答
0

python 中的所有内置函数都应该使用(). 所以使用

if choice.lower()!="q":

只有这样才能调用该函数。否则总是返回值False,因为没有执行任何函数

于 2013-04-20T11:24:43.600 回答
0
  1. 替换lowerlower()
  2. 否则使用float(fisrtnum) + float(secnum)它只会连接字符并为 '2'+'3' 提供 23
于 2013-04-20T11:25:51.913 回答