Python while循环无法正常运行(初学者)

时间:2013-04-20 11:17:46

标签: python-3.x

我仍然对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!")

P.S。 - 这是我在这里的第一篇文章,所以如果我没有正确地做某事,请告诉我。

谢谢!

JT

3 个答案:

答案 0 :(得分:3)

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

我认为你的意思是低(),而不是更低;)

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

  1. lower替换为lower()
  2. 使用float(fisrtnum) + float(secnum),否则它将连接char并为23提供'2'+'3'