-1

what will be the output of this code and why

options = "year2"
options1 = "semester1"
if (options == "year1") and (options1 == "semester1"):
    print("a")
elif (options == "year1" or "year3" or "year4") and (options1 == "semester2" or "semester3"):
    print("b")
elif (options == "year2" or "year3" or "year4") and (options1 == "semester1"):
    print("c")
else:
    print("d")
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
kepla
  • 11
  • 1
  • 2
    Hint: The value `"year3"` by itself is considered [truthy](https://stackoverflow.com/q/39983695/328193), as is any other string. As a result, the second condition (the first `elif`) will *always* be true. – David Jul 11 '22 at 13:57
  • 1
    `(options == "year1" or "year3" or "year4")` will execute without error, but will always return `TRUE`. Instead perhaps `(options == "year1" or options == "year3" or options == "year4")` is what was meant? Note that `OR` and `AND` evaluate individual conditions, not a condition and a bunch of strings. – JNevill Jul 11 '22 at 13:59

1 Answers1

0

You are looking for something like

options in ("year1", "year2", ...)

The Misunderstanding here is, that you connect with or

<options == "year1"> or <"year3"> ...

<options == "year1"> might or migth not be true <"year3"> is equivalent to bool("year3") which will be always true

rikisa
  • 301
  • 2
  • 9