x=[1,2,3,4,5,6]
for y in x:
if y %2 == 0:
print (y)
elif y %3 == 0:
print ("y")
elif y %3 and y %2 ==0:
print ("Divisible by 2 and 3!")
else:
print ("Odd number!")
I was trying to find out the even and odd numbers between 1 to 6. All worked out fine except the integer 6 where I need it to print out that the integer 6 is divisible by 2 and 3. How can I fix the error?
The test for divisible by 2 and 3 should come first, so for 6 you have y %3==0 and y %2 ==0
evaluated instead of y %2 == 0
:
for y in x:
if y % 3 == 0 and y % 2 == 0:
print ("Divisible by 2 and 3!")
elif y % 2 == 0:
print (y)
elif y % 3 == 0:
print (y)
else:
print ("Odd number!")