【发布时间】:2016-10-24 17:00:47
【问题描述】:
我的目标:我试图让用户输入他们自己的查询以获取故障排除系统。如果用户的输入具有在“关键字”数组中找到的关键字,则从“答案”数组中的相同索引给出解决方案。
问题:没有语法错误,而是逻辑错误。对于 'keywords' 数组中的第一个和第二个索引,如果输入了这个关键字,则给出正确的解决方案。但是,对于 'keywords' 数组中的第三个和第四个索引,它会从 'answers' 数组中的不同索引输出错误的解决方案。
我的代码:
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
i = 0
while i <= 5:
user_query = str(input('What\'s the problem?\n>> ')).lower()
for keyword in keywords:
while keyword[i] not in user_query:
i = i + 1
if keyword[i] in user_query:
print(answers[i])
i = 10
break
if i >= 5:
print('contact the supplier')
break
【问题讨论】:
标签: python arrays indexing while-loop counter