Python Forum
Output with none, print(x) in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output with none, print(x) in function
Do you have any suggestions how to change this None in an output value?

I have read that it is because of print(x), but... if I delete or change print(x) to return x, for loop will end at 1 number. I don't want to have list [] output just normally print(x) in function.

def oddNumbers(l=1, r=7):
    for x in range(l, r):
        if x % 2 == 0:


I find out to pack it into list and then unpack using loop, but to be honest I do not like it, do you have any sugesstions?
def oddNumbers(l=1, r=7):
    # Write your code here
    lista = []
    for x in range(l, r):
        if x % 2 == 0:
    return lista
arr = oddNumbers()

for i in arr:
The problem lies in this line:

When oddNumbers is executed, you get 1,3,5 as expected (because the oddNumbers function prints, rather than returns, the odd numbers). However, because the function has no return value, when print() is executed on oddNumbers, you get None.

If you change the line to:
You get your desired output:
1 3 5
If you want the function to generate the list of odd numbers for you to use later, you can use a list comprehension like this:

oddnumbers = [x for x in range(1,7) if x %2 != 0]
I think putting a print statement in odd_numbers() is bad design. It makes the function useless. I cannot use your odd_numbers() to get a list of odd numbers for the program I am writing. I don't want the printing numbers side effect messing up the output in my program.

For something like this maybe I want a generator.
def odd_numbers(start=1, end=None):
    if start % 2 == 0:
        start += 1
    while True:
        if end and start > end:
        yield start
        start += 2

# If I want to print odd numbers
print('Count from 11 to 21 by 2')
for x in odd_numbers(11, 21):

# If I want a list of odd nmbers
a = list(odd_numbers(1, 7))
b = [x for x in odd_numbers(13, 21)]
print('Combined lists =', a+b)

# Or an unknown number of odd numbers
odds_gen = odd_numbers()
odds = []
while sum(odds) < 100:
print(f'sum{odds} = {sum(odds)}')
You would never do something like this for odd numbers. They are way too easy to generate ad hoc. But you might do this for a different sequence that is not so easy to generate and that might have several different uses.

Possibly Related Threads…
Thread Author Replies Views Last Post
  output correction using print() function afefDXCTN 3 3,553 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 284 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  python prints none in function output chairmanme0wme0w 3 394 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 946 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Print output not working xninhox 7 1,406 Jan-16-2021, 09:42 AM
Last Post: xninhox
  How to append to list a function output? rama27 5 1,567 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  print function help percentage and slash (multiple variables) leodavinci1990 3 912 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Invalid syntax on print function DoctorSmiles 2 1,310 Jul-12-2020, 07:39 PM
Last Post: DoctorSmiles
  Print output in single file using pramika loop deepakkhw 1 886 Jul-11-2020, 11:57 AM
Last Post: j.crater
  print output on Jenkins Pench 1 1,950 Jun-04-2020, 06:55 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020