简体   繁体   中英

While loop is not iterating over the correct solution in array

My goal: I am trying to get the user to type in their own query for a troubleshooting system. If the user's input has a keyword that is found in the 'keywords' array, a solution is given from the same index in the 'answers' array.

The problem: There are no syntax errors but a logic error. For the first and second index in the 'keywords' array, if this keyword is inputted then a correct solution is given. However, for the third and fourth index in the 'keywords' array, it outputs the wrong solution from a different index in the 'answers' array.

My code:

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

You must remember that in for keyword in keywords: , keyword is a string, and indexing it via i is pulling out individual letters. Instead you want to do something like:

for keyword in keywords:
    if keyword in user_query:
        # Handle things here

or

for i in range(len(keywords)):
    if keyword[i] in user_query:
         # Handle things here

This second approach will allow you to reference the corresponding entry in the answers array, so it is what I'd recommend.

You will still need to clean things up and make sure the user is entering the query at the correct location in the code. My guess is that the code you want (though you should verify) is:

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')

This code is using an else block attached to the for loop. To understand what is happening, I suggest you read up on them here .

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.")

Here is another option that uses the lists built in index function instead of needing the for loop.

I think this might work a little better:

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))

Example output:

What's the problem?
>> screen cracked
replace screen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM