2

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

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

1 Comment

and this could easily be extended to return a list of solutions in case multiple keywords are matched, if that's what you are looking to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.