Why does my code not print values divisible by 2?


#1

Question

Why does my code not print values divisible by 2?

Answer

There are a couple different reasons this may happen, so go through the checklist below:

  1. Did you indent your code properly? Remember that Python uses indentation (2 spaces per indentation level) to denote blocks of code.
  2. Is there a colon after your for loop and if statement? Leaving this out causes a syntax error.
  3. What value are you printing inside of your if statement if the number is divisible by 2? If you’re printing the list name, that’s not quite right - print the variable you just checked.
  4. Did you use = or == to check if a number is divisible by 2? Remember that = is used to assign value, so that’s not what we use in an if statement.

FAQ: Learn Python: A Day at the Supermarket - Control Flow and Looping