r/PythonLearning • u/Hunter_z39 • 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) 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.
You have to check value%2==0 instead ‘odd_numbers’ list.
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)
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 it5
2
2
1
1
u/FoolsSeldom Sep 02 '25
if(odd_numbers % 2 == 0) is not valid:
odd_numbersreferences alistobject, and you cannot apply the modulo,%, operatorMissing a
:from the endMissing 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
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