Python错误:“列表对象没有属性”.upper“

时间:2016-11-03 20:02:32

标签: python python-3.x

我目前正在尝试编写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'

并且有办法修复我的程序以使其正常工作吗?

感谢您的回复。

1 个答案:

答案 0 :(得分:3)

提示:

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

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

    reply.upper() in greetingsInUpCases

允许您确定reply 是否在当前列表中。您可以在if声明中使用它:

    if reply.upper() in greetingsInUpCases: