-1
compareusr = str(input())

compare = "austin" or "cloud"
if compare == compareusr:
    print("it worked")
else:
    print("it didnt work")

This may be a stupid question. I’m not the most fluent in python however I thought I knew enough for this comparison work.

Does anyone know why when the input=cloud the code does not work?!?!? It works with and, but why does it not work with or?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 6
    Possible duplicate of [Python testing whether a string is one of a certain set of values](https://stackoverflow.com/questions/17902492/python-testing-whether-a-string-is-one-of-a-certain-set-of-values) – Georgy Dec 09 '18 at 09:41
  • 1
    Possible duplicate of https://stackoverflow.com/questions/44612144/logical-operators-in-python – kantal Dec 09 '18 at 09:43
  • 1
    `compare = "austin" or "cloud"` is in effect same as `compare = "austin"`. Create a list and check condition with `in`. – Austin Dec 09 '18 at 09:44
  • 1
    Also: [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Georgy Dec 09 '18 at 09:45
  • 1
    Possible duplicate of [Logical operators in Python](https://stackoverflow.com/questions/44612144/logical-operators-in-python) – Pransh Tiwari Dec 09 '18 at 10:00
  • 1
    @PatrickArtner in this case `or` functions as the [elvis operator](https://stackoverflow.com/a/48813117/10630900) – Minn Dec 09 '18 at 14:24

1 Answers1

0

Operator or returns logical value. In your code compare is equal to True because the strings are not empty. Conversion input to str is redundant.

compareusr = input()

compare = ["austin", "cloud"]
if compareusr in compare:
    print("it worked")
else:
    print("it didnt work")
The Godfather
  • 4,235
  • 4
  • 39
  • 61
Kuba
  • 123
  • 6
  • Actually, `or` functions as the elvis operator in this case. This means that `compare` is equal to `"austin"` because it is not the `None` type. – Minn Dec 09 '18 at 14:20