'bea'没有定义?

时间:2014-06-30 12:06:06

标签: python python-2.7

def money():
    cashin = input("Please insert money: £1 Per Play!")
    dif = 1 - cashin
    if cashin < 1.0:
        print ("You have inserted £", cashin, " you must insert another £", math.fabs(dif))
    elif cashin > 1.0:
        print ("Please take your change: £", math.fabs(dif))
    else:
        print ("You have inserted £1")
    menu()

def menu():
    print ("Please choose an ARTIST: <enter character ID>")
    print ("<BEA> - The Beatles")
    print ("<MIC> - Michael Jackson")
    print ("<BOB> - Bob Marley")
    cid = input()
    if cid.upper == ("BEA"):
        correct = input("You have selected the Beatles, is this correct? [Y/N]:")
        if correct.upper() == ("Y"):
            bea1()
        else:
            menu()
    elif cid.upper() == ("MIC"):
        correct = input("You have selected Michael Jackson, is this correct? [Y/N]:")
        if correct.upper() == ("Y"):
            mic1()
        else:
            menu()
    elif cid.upper() == ("BOB"):
        correct = input("You have selected Bob Marley, is this correct? [Y/N]:")
        if correct.upper() == ("Y"):
            bob1()
        else:
            menu()
    else:
        print ("That is an invalid character ID")
        menu()

这是我的自动点唱机代码的一部分

如果我在'CHOOSE AN ARTIST'输入中输入'bea',我的代码会给我一个错误,即bea未定义。但我不是试图将它用作函数或变量?我想使用if语句根据用户输入决定做什么

通常这对我有用,我不知道问题是什么。这只是我的代码的一部分,如果你需要看到更多让我知道

1 个答案:

答案 0 :(得分:4)

在Python 2.x中,要从提示中获取字符串,请使用raw_input()代替input()

cid = raw_input()

Python 2中的input函数与Python 3中的input不等效。它不返回字符串,但评估。在您的情况下,键入bea并尝试将bea作为表达式进行评估。但是这个名字没有定义。

正如input的文档中提到的那样:

  

相当于eval(raw_input(prompt))

另外,正如@frostnational在他的评论中指出的那样,你忘了在下一行实际调用upper方法,所以你试图将方法本身与字符串进行比较。你想要

if cid.upper() == "BEA":