虽然循环没有遍历数组中的正确解决方案
While loop is not iterating over the correct solution in array
我的目标: 我试图让用户输入他们自己的查询以查找故障排除系统。如果用户的输入具有在 'keywords' 数组中找到的关键字,则从 'answers' 数组中的相同索引给出解决方案。
问题:没有语法错误,只有逻辑错误。对于'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
你必须记住,在 for keyword in keywords:
中,关键字是一个字符串,通过 i
对其进行索引是提取单个字母。相反,你想做这样的事情:
for keyword in keywords:
if keyword in user_query:
# Handle things here
或
for i in range(len(keywords)):
if keyword[i] in user_query:
# Handle things here
第二种方法将允许您引用 answers
数组中的相应条目,因此这是我推荐的方法。
您仍然需要清理并确保用户在代码中的正确位置输入查询。我的猜测是你想要的代码(虽然你应该验证)是:
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
user_query = str(input('What\'s the problem?\n>> ')).lower()
for i in range(len(keywords)):
if keyword[i] in user_query:
print(answers[i])
break
else:
print('contact the supplier')
此代码使用附加到 for
循环的 else
块。要了解发生了什么,我建议您阅读它们 here。
我认为这可能会更好一些:
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
responses = {k:v for k,v in zip(keywords,answers)}
def getAnswer(query, solutions):
for keyword in solutions:
if keyword in query:
return solutions[keyword]
return "Contact the supplier"
user_query = str(input('What\'s the problem?\n>> ')).lower()
print(getAnswer(user_query,responses))
示例输出:
What's the problem?
>> screen cracked
replace screen
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
query = input('What\'s the problem?\n>> ').lower()
try:
print(answers[keywords.index(query)])
except ValueError:
print("Contact the supplier.")
这是另一个选项,它使用内置索引函数的列表而不需要 for 循环。
我的目标: 我试图让用户输入他们自己的查询以查找故障排除系统。如果用户的输入具有在 'keywords' 数组中找到的关键字,则从 'answers' 数组中的相同索引给出解决方案。
问题:没有语法错误,只有逻辑错误。对于'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
你必须记住,在 for keyword in keywords:
中,关键字是一个字符串,通过 i
对其进行索引是提取单个字母。相反,你想做这样的事情:
for keyword in keywords:
if keyword in user_query:
# Handle things here
或
for i in range(len(keywords)):
if keyword[i] in user_query:
# Handle things here
第二种方法将允许您引用 answers
数组中的相应条目,因此这是我推荐的方法。
您仍然需要清理并确保用户在代码中的正确位置输入查询。我的猜测是你想要的代码(虽然你应该验证)是:
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
user_query = str(input('What\'s the problem?\n>> ')).lower()
for i in range(len(keywords)):
if keyword[i] in user_query:
print(answers[i])
break
else:
print('contact the supplier')
此代码使用附加到 for
循环的 else
块。要了解发生了什么,我建议您阅读它们 here。
我认为这可能会更好一些:
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
responses = {k:v for k,v in zip(keywords,answers)}
def getAnswer(query, solutions):
for keyword in solutions:
if keyword in query:
return solutions[keyword]
return "Contact the supplier"
user_query = str(input('What\'s the problem?\n>> ')).lower()
print(getAnswer(user_query,responses))
示例输出:
What's the problem?
>> screen cracked
replace screen
answers = ['dry it out','replace screen','delete any apps that are not needed','restart it']
keywords = ['wet','cracked','download','unresponsive']
query = input('What\'s the problem?\n>> ').lower()
try:
print(answers[keywords.index(query)])
except ValueError:
print("Contact the supplier.")
这是另一个选项,它使用内置索引函数的列表而不需要 for 循环。