#If statement

7 messages · Page 1 of 1 (latest)

drifting void
#

I'm curious of the following:

list = [2,2,3,5,4,4,6,7]

for value in list:
if value == (2 or 4):
print(value)

Why does the program only print for 2, and not 4? Is the condition equivalent to: if (value == 2) or 4?

hot tartan
#

!eval 2 or 4

latent basinBOT
hot tartan
#

So you're doing if value == 2

#

!tip if

latent basinBOT
#
Why Doesn't This Condition Work?
if age >= 12 and < 18:

and as well as or delineate distinct conditions. So you need to specify the variable in each condition in the statement. Like this

if age >= 12 and age < 18:

Additionally you can chain the greater than, less than, greater than or equal to, and less than or equal to. Like this

if 12 <= age < 18:
hot tartan
#

Alternatively you could do if value in (2, 4):. Which should give you the result you want