“bea”未定义?

2024-03-28 13:53:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

这是我的自动存储塔代码的一部分

如果我在“选择艺术家”输入中键入“bea”,我的代码会给我一个错误,即没有定义bea。但我不是想把它当作函数或变量?我想使用if语句根据用户的输入来决定要做什么

通常这对我有效,我不知道问题出在哪里。这只是我的一部分代码,如果你需要看到更多让我知道


Tags: youinputifishaveupperelsemenu
1条回答
网友
1楼 · 发布于 2024-03-28 13:53:54

在Python2.x中,要从提示符获取字符串,请使用^{}而不是^{}

cid = raw_input()

Python 2中的input函数与Python 3中的input函数不同。它不返回字符串,但对其求值。在您的例子中,您键入bea,它尝试将bea作为表达式求值。但是这个名字没有定义。你知道吗

input的文档所述:

Equivalent to eval(raw_input(prompt)).

另外,正如@frostnational在其注释中所指出的那样,您忘记了在下一行实际调用upper方法,因此您试图将方法本身与字符串进行比较。你想要什么

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

相关问题 更多 >