r/PythonLearning • • Sep 02 '25

Help Request HELP ME

why is this code is not working

'''
Task 3 — Odd numbers 1–19

Make a list of odd numbers from 1 to 19 (use a step).
Self-check: 10 numbers, all odd.
'''
odd_numbers = []
for value in range(1, 20, 2):  
# Using step of 2 to get odd numb
    odd_numbers.append(value)
if(odd_numbers % 2 == 0)
print(odd_numbers)    
0 Upvotes

12 comments sorted by

1

u/Hunter_z39 Sep 02 '25

i know there is simpler way but i am trying to learn every way possible cuz i am noobie

4

u/EmbarrassedTask479 Sep 02 '25

In your code odd_numbers is a list, not a number, so odd_numbers % 2 is invalid , just remove that if and directly print(odd_numbers).

1

u/SpecialistTicket1 Sep 02 '25

So there are couple of mistakes.

  1. You have to check value%2==0 instead ‘odd_numbers’ list.

  2. If value % 2==0 is true, then you need to append that value to the empty list which you have created: odd_numbers.append(value)

  3. At last you can print the list outside for loop.

3

u/BranchLatter4294 Sep 02 '25

Try moving the print statement to inside the loop and having it print value. Get rid of the if statement. Then see what you have in your list.

3

u/Glittering_Sail_3609 Sep 02 '25
odd_numbers = []
for value in range(1, 20, 2):  
# Using step of 2 to get odd numb / this comment breaks intentation level, add tab before it or move it before the for loop
    odd_numbers.append(value)
if(odd_numbers % 2 == 0) # You missed ':' here
print(odd_numbers) # again, incorrect intentation, please insert tab before it

5

u/deceze Sep 02 '25

Fixing the if syntax won't help; the % operation is pointless.

2

u/siddh0206 Sep 02 '25

indentation errors bud

2

u/SCD_minecraft Sep 02 '25

You missed : in if, and lists don't support modulo

1

u/cully_buggin Sep 02 '25

I’m new. Only sorta understand. But I thought odd numbers == 1

1

u/FoolsSeldom Sep 02 '25

if(odd_numbers % 2 == 0) is not valid:

  • odd_numbers references a list object, and you cannot apply the modulo, %, operator

  • Missing a : from the end

  • Missing an indented statement under the if

If you want to check the list contains only odd numbers and there are 10 of them, you can use:

if len(odd_numbers) == 10 and all(n % 2 == 1 for n in odd_numbers):

1

u/Minute_Journalist593 Sep 03 '25

bro have you ever heard that odd number is divided by 2 and reminder is 0 and after if block you have missed : and after if your indentation is missing