A.red
A.red

Reputation: 3

Please spot the error in my Python code

I have to code for a trouble shooting system, which uses keywords in the user input to provide a solution from a text file. I have a list of If statements, which directs the program to read specific lines from a textile and present them as solutions according to the specific Keywords present in the user input.

However, whatever the user input is, the program seems to display the first two lines in the text file (the program seems to be stuck on the first if statement).

Keywords = ['screen', 'display', 'audio', 'sound',]
Keywords2 = ['cracked', 'broken', 'water', 'frozen', 'unresponsive']

# Collecting the user's problem using user input

problem = input(str("Please enter your problem:")).split()# Assigns the user input to the variable 'progarm' and converts the user input into a string

# Presenting solutions to user if corresponding Keywords are present in the users input 'problem'
if any(i in Keywords or Keywords2 for i in problem): # This while loop checks that any of the keywords (i) in the lists (Keyword or Keywords2) are present in the user's input (problem), which has been splitted into individual words (indicated by spacing - i)
    if Keywords[0] or Keywords[1] and Keywords2[0] or Keywords2[1] in problem:
        read(0)
        read(1)
    elif Keywords[0] or Keywords[1] and Keywords2[3] or Keywords2[4] in problem:
        read(3)
    elif Keywords2[2] in problem:
        read(2)
    elif Keywords[2] or Keywords[3] and Keywords2[5] or Keywords2[6] in problem:
        read(4)
        read(0)

When I enter 'the phone fell in water', it should detect the Keyword 'water', which is Keywords2[2], and read the second line in the text file. Instead, it reads the lines directed in the first if statement.

Please enter your problem:My phone fell into water
Get it repaired at a reliable repair shop

You will need to buy a new screen and a new screen protector

Any suggestions to improve the code and make it work would be much appreciated!

Upvotes: 0

Views: 127

Answers (1)

รยקคгรђשค
รยקคгรђשค

Reputation: 1979

I think the problem might be with the improper usage of in.

You may want to do something like this:

any(i in Keywords or i in Keywords2 for i in problem) 

instead of

any(i in Keywords or Keywords2 for i in problem)

Similarly, for the inner if and elif statements.

For example:

if (Keywords[0] in problem or Keywords[1] in problem) and (Keywords2[0] in problem or Keywords2[1] in problem):

instead of

if Keywords[0] or Keywords[1] and Keywords2[0] or Keywords2[1] in problem:

Upvotes: 1

Related Questions