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?
Many novice programmers think that if you write
if a=="ноль" or "нулевой":
а 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:
This is exactly what happens to you.