0

I was trying to write a range function below however it printed out "None" after every line. Please help fix them.

def odd_number(n):
    num = []:
    for x in range(1, n+1):
        if x % 2 != 0:
            num.append(x)
    print(num)

print(odd_numbers(5))
print(odd_numbers(10))

When I hit print the results came out as

[1, 3, 5]
None
[1, 3, 5, 7, 9]
None

instead of

[1, 3, 5]
[1, 3, 5, 7, 9]

Please help explain what I was doing wrong and help with fixing it.

Thank you

Don
  • 11
  • 2
  • Additionally, you could get odd or even numbers a lot easier by using `list(range(1, n+1, 2))` for odd numbers and start the range with two for even number, here I'm using step of 2 in the range function. Finally, You either print results from the function or return the list and print the function result, you shouldn't do both. – Abdelrahman May 02 '23 at 00:54
  • Your function returns `None` (since you don't explicitly `return` anything, it will be the implicit return value). Hence, since you call the function and print the result of the call, it will print `None` – juanpa.arrivillaga May 02 '23 at 03:19

0 Answers0