Python错误:“list object没有属性”.upper”

2024-05-09 02:54:00 发布

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

我目前正在尝试用python编写一个代码,如果您的回答在可能的答案列表中,那么该代码将对您做出响应。程序示例如下。

def responce():
    greetings = ["Hello","Hi","Nice to see you!","Greetings","How's it going?","How are you doing?","What's new?","How's your day going?","Hey!"]
    print("\n")
    reply = input(": ")
    lenrep = len(reply)
    tempstore = []
    for i in range(0,lenrep):
        tempstore.append(i)
        z = 0
        while z < 9:
            tempgreet = greetings[z]
            if tempstore.upper() == tempgreet.upper():
                reply = ""
                tempstore = []
                temprandno = random.randint(0,2)
                addon = ["what do you want to know?", "what do you want to talk about?", " "]
                addontext = addon[temprandno]
                text(greet(), + " " + addontext)
                z += 1
        if i == " ":
            tempstore = [] 

现在我的问题是,为什么会出现错误

AttributeError: list object has no attribute 'upper'

有没有办法修正我的程序使之生效?

谢谢你的回复。


Tags: to代码addon程序youifreplyupper
1条回答
网友
1楼 · 发布于 2024-05-09 02:54:00

提示:

    greetingsInUpCases  = [elem.upper() for elem in greetings ]

将问候语列表中的所有字符串转换为大写字母,并将表达式

    reply.upper() in greetingsInUpCases

允许您决定reply是否在当前列表中。您可以在if语句中使用它:

    if reply.upper() in greetingsInUpCases:

相关问题 更多 >

    热门问题