Python and the lowercase “or” problems

Question:

Hello everyone, programming in Python, I have a problem with the if statement and its under the or function when working with str looks something like this

if a=="ноль" or "нулевой":
    print(0)
elif a=="один" or "единичный":
    print(1)
# выводит лишь 0 при любых значениях a

What should I do and how can I fix it?

Answer:

Many novice programmers think that if you write

if a=="ноль" or "нулевой":

then а will be compared alternately with both parts – both with "ноль" and with "нулевой" .

if a=="ноль" или if a == "нулевой":

In fact, there will be only one comparison: a == "ноль" . The second part ( "нулевой" ) is simply evaluated for a boolean result, which in this case is always True ( as for any other string ). And since True, then execution always goes to the next line of print (0) regardless of the value of а .

In other words, the line if a=="ноль" or "нулевой" equivalent to the line if "нулевой" . And since the result of such an expression is always True, then this line is not needed either. Based on the above, all of your four lines are equivalent to one:

print(0)

This is exactly what happens to you.

Scroll to Top