Search code examples
pythonpython-3.xnonetype

NoneType occuring when I run my code


Why does this code return None?

 def html_list(inputs_list):
    print("<ul>")

    for html in inputs_list:
        html = ("<li>" + html + "</li>")
        print(html)

    print("</ul>")

bam = ['boom','bust']

print(html_list(bam))

Solution

  • Your function has print calls within it, but does not return anything. If a function completes without returning, it really returns None.

    This means that if you call the function inside a print statement, it will run, do the prints within the function, but return None which will then get passed into the print() function - so this is why you get the intended output and then a "None" at the end.

    You can also see this through a more simple example:

    >>> def f():
    ...     print("inside f")
    ... 
    >>> print(f())
    inside f
    None