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))
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))
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
- first string
- second string
That is, the string's first line should be the opening tag. Following that is one line per element in the source list, surrounded by- and
tags. The final line of the string should be the closing tag
. – Richard Pak Jan 28 '18 at 08:12