这段代码的目的是通过用户输入检查,如果检查是否有任何单词匹配字典上的单词,然后给出一个与匹配的第一个单词相关的回答,如果没有回答“我很好奇告诉我更多”。我的问题是我不能遍历列表并打印一个响应。你知道吗
def main():
bank = {"crashed":"Are the drivers up to date?","blue":"Ah, the blue screen of death. And then what happened?","hacked":"You should consider installing anti-virus software.","bluetooth":"Have you tried mouthwash?", "windows":"Ah, I think I see your problem. What version?","apple":"You do mean the computer kind?","spam":"You should see if your mail client can filter messages.","connection":"Contact Telkom."}
def welcome():
print('Welcome to the automated technical support system.')
print('Please describe your problem:')
def get_input():
return input().lower().split()
def mainly():
welcome()
query = get_input()
while (not query=='quit'):
for word in query:
pass
if word in bank:
print(bank[word])
elif not(word=="quit"):
print("Curious, tell me more.")
query = get_input()
mainly()
if __name__=="__main__":
main()
在你的代码中几乎没有错误。第一个,当你启动脚本时,你运行main,加载一个本地的disctionary“bank”,它不存在于函数之外。当函数结束时,它“主要”运行,但不记得字典。你知道吗
第二个,当你使用词汇结构时,你不需要循环检查所有的元素。您可以改为使用函数听写你知道吗
我可以向您提出以下解决方案:
在这种情况下银行。获取(query,None)如果单词存在,则返回句子,否则返回None。你知道吗
您还可以将其简化为:
这是因为如果它存在,句=你想显示什么,如果它不存在,它会显示你想要的错误信息
希望对你有帮助
相关问题 更多 >
编程相关推荐