samedi 28 janvier 2017

Python. I can't figure out what's wrong with this IF/ELIF/ELSE statement [on hold]

sentence = input ("Enter your sentence: ")
sentence = sentence.lower()
if "phone" in sentence:
    if "display" in sentence:
        print ("Stuff")
    elif "water" in sentence:
        print ("Stuff2")
    else:
        print ("Stuff3")
elif "tablet" in sentence:
    if "display" in sentence:
        print ("Stuff4")
    elif "water" in sentence:
        print ("Stuff5")
    else:
        print ("Stuff6")
else:
    print ("Stuff7")

So, this is the script that I came up with for creating a Trouble-shooter based on an input from the user. For example, the user would say "My phone doesn't have a display" and this will print "Stuff".

But it doesn't actually work like that and I don't know why. The user will always get the output "Stuff", no matter if he included "display" in his sentence or not. The same goes if the user wrote "tablet" and "water". It will come up with "Stuff4" instead of "Stuff 5".

Basically what I'm trying to do is to look for 2 specific words in the sentence and if they are included it will print a solution to the problem.

Can someone please tell me if I'm doing something wrong or if there is a better method to do this.

Thank you.

[Additional: The script should identify firstly the type of device the user should include in their sentence (e.g. phone, pc, tablet) then it will look for a word related to an issue (e.g. "water" would relate to the device getting wet and stopped working).]

Aucun commentaire:

Enregistrer un commentaire