аватар on5fydu8@gmail.com · 01.01.1970 03:00

The comparison of the variable with several values ​​through OR works incorrectly

"

I had a problem with the if operator and its function or . When working with the Str, it looks something like this

   if  a ==  "zero" "  or  " "zero" ":   print  ( 0 )   elif  a ==  "" "one"   or  :   print  ( 1 )  # removes only 0 for any values ​​for any values ​​for any values A      

What should I do and how to fix it?

"

аватар on5fydu8@gmail.com · 01.01.1970 03:00

Many beginner programmers think that if you write

  un  a == " zero "  or  :     

then will be compared alteately with both parts - both with "" zero "" zero "" "" zero "" .

   f  a ==  "zero" "zero" or the "  if  a == :     

in fact, the original line is nothing but

   if  (A ==  "" zero "" )  ry  ():     

therefore there will be only one comparison: a ==" zero "" . The second part ( "" zero "" ) is simply evaluated at the logical result, which in this case is always true (). And if it is true, then the execution always proceeds to the following line print (0) regardless of the value of A . If "" zero "" . And T.K. The result of such an expression is always true, then this line is not needed. Based on the foregoing, all your four lines are equivalent to one:

   print  ( 0 )     

this is exactly what happens.

Latest

Similar