Skip to content Skip to sidebar Skip to footer

If Statement With Or Not Working As Expected

My program first asks the user what is wrong with their mobile phone and when the user writes their answer, the program detects keywords and outputs a solution. I used 'or' stateme

Solution 1:

I think you mis-understand or. In programming, or is slightly different from in English. x or y in z means "is x True or is y in z?". If the first argument (x) is non-empty, that means that it evaluates to True. No check is made for anything's presence in z. If x is empty, it evaluates to False and there is a check for y's presence in z, but not x's presence. You need to create a new test for each one. Also, you used problem instead of problem1 in your second elif:

if "cracked" in problem1 or "screen broke" in problem1:
    ...
elif "water" in problem1 or...
    ...

Post a Comment for "If Statement With Or Not Working As Expected"