dimanche 19 mars 2017

Please spot the error in my code (python)

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', 'buttons', 'battery', 'wifi', 'internet'] # This is a list of Keywords that identifies the specific area of the phone with the problem
Keywords2 = ['cracked', 'broken', 'water', 'frozen', 'unresponsive', 'not playing', 'not working', 'not charging', 'not connected'] # This is a list of Keywords to help specify the problem

# Collecting the user's problem using user input

print("Trouble Shooting Program for Mobile Phones") # This informs the user what the program does
problem = input(str("Please enter your problem:")).split()# Assigns the user input to the variable 'progarm' and converts the user input into a string
def read(n):
    f = open('solutions.txt', "r")
    lines = f.readlines()
    x = lines[n]
    print(x)    

# 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)
    elif Keywords[4] and Keywords2[1] or Keywords2[6] in problem:
        read(0)
    elif Keywords[5] or Keywords2[7] and Keywords2[6] in problem:
        read(5)
        read(0)
    elif Keywords[6] or Keywords[7] and Keywords2[1] or Keywords2[6] or Keywords2[8] in problem:
        read(6)

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

Thank you for using our trouble shooring service, hope this helped!

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

Aucun commentaire:

Enregistrer un commentaire