0

This is my code and the problem I am having is that it is not sequencing from the IF statements to the ELIF is there any way to fix this? Basically it works without the "or" part but it doesn't when you add the or to it. This is different to the other question as it is using strings whereas the other question uses integers.

import time
print "Welcome to the Troubleshooting Program"
time.sleep(0.2)
query=raw_input("What is your query? ")
querystr=str(query)
if "screen" or "Screen" in querystr:
    in_file=open("Screen.txt", "r")
    screen_answer=in_file.read()
    print "We have identified the keyword Screen, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

elif "wet" or "Wet" or "water" or "Water" in querystr:
    in_file2=open("Wet.txt", "r")
    screen_answer=in_file.read()
    print "We have identified that the device has been in contact with water, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

elif "Bath" or "bath" or "sink" or "Sink" or "toilet" or "Toilet" in querystr:
    in_file3=open("Wet.txt", "r")
    screen_answer=in_file.read()
    print "We have identified that the device has been in contact with water, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

elif "battery" or "Batttery" or "charge" in querystr:
    in_file=open("Battery.txt", "r")
    screen_answer=in_file.read()
    print "We have identified that there is an issue with the devices battery, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

elif "speaker" or "Speaker" or "Sound" or "sound" in querystr:
    in_file=open("Speaker.txt", "r")
    screen_answer=in_file.read()
    print "We have identified that there is an issue with the devices speakers, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

elif "port" or "Port" in querystr:
    in_file=open("Port.txt", "r")
    screen_answer=in_file.read()
    print "We have identified that there is an issue with the devices ports, here are your possible solutions:"
    time.sleep(0.5)
    print screen_answer

else:
    repeat=raw_input("Do you have another query? Y/N")
    if repeat =="Y" or "y":
        queryloop()
    else:
        print "Thank you!"

Thank you for your time.

  • 2
    "screen" is evaluated as True, so the first condition is always met. You want `"screen" in querystr or "Screen" in querystr`. Or `"screen" in querystr.lower()`. – S. de Melo Nov 17 '16 at 12:14
  • 1
    Thank you very much, this has fixed my code. – Alex Mantle Nov 17 '16 at 12:19
  • Between every OR is a condition. A condition must evaluate to true or false, but all you have is a string, which will always evaluate to true, just because that's how python works. Looks like you're trying to check if something is equal to another thing in which case you should use thing == "another thing" Or you could just by pass all of your ORs and use a function that returns a boolean. That's what I would do. The function can take a list of strings and go through each of them and check to see if they're in the query – The-IT Nov 17 '16 at 12:22
  • Also, as another note, Python and SQL have differing syntaxs. You may wanna get more familiarised with Python syntax... – The-IT Nov 17 '16 at 12:23

1 Answers1

0

Issue Here:

Your condition:

if "screen" or "Screen" in querystr:

will always be True because:

"screen" or "Screen" in querystr

will always return "screen" and your if will treat it as True.

How to do it?

If you want to check whether some words are in querystr or not. You may do it like:

if any(word in querystr for word in ["screen", "Screen"])

In this particular case, since just want to check the condition ignoring the case, it could be written as:

if "screen" in querystr.lower():
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126