Variable comparison with multiple values using or works incorrectly
📁 Advanced Python Topics, example
"
I have a problem with the if statement and its or function. When working with str it looks something like this
if a==""zero"" or ""null"": print(0)elif a==""one"" or ""single"": print(1)# prints only 0 for any values of aWhat should I do and how to fix it?
"
Many novice programmers think that if you write
if a==""ноль"" or ""нулевой"":then a will be compared sequentially with both parts - both with ""ноль"" and with ""нулевой"".
if a==""ноль"" or if a == ""нулевой"":In fact, the original line is nothing more than
if (a==""ноль"") or (""нулевой""):Therefore, there will be only one comparison: a == ""ноль"". The second part (""нулевой"") is simply evaluated for a boolean result, which in this case is always True ().
Log in to leave an answer