Joe*_*_49 -2 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 = []
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,为什么会出现错误
AttributeError: list object has no attribute 'upper'
Run Code Online (Sandbox Code Playgroud)
有没有办法修复我的程序以使其正常工作?
感谢您的任何回应。
提示:
greetingsInUpCases = [elem.upper() for elem in greetings ]
Run Code Online (Sandbox Code Playgroud)
将问候语列表中的所有字符串转换为大写字母,以及表达式
reply.upper() in greetingsInUpCases
Run Code Online (Sandbox Code Playgroud)
允许您决定 是否reply 在当前列表中。您可以在if声明中使用它:
if reply.upper() in greetingsInUpCases:
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14467 次 |
| 最近记录: |