Outputting HTML unordered list python
Solution 1
You could do something like that:
def print_ul(elements):
print("<ul>")
for s in elements:
ul = "<li>" + str(s) + "</li>"
print(ul)
print("</ul>")
toppings = ['mushrooms', 'peppers', 'pepparoni', 'steak', 'walnuts', 'goat cheese', 'eggplant', 'garlic sauce'];
print_ul(toppings)
There were some problems with your original code:
- you did not call the function, so no wonder it didn't do anything
- even if you did, the function did not actually print anything, it just returned some values
- the function didn't really take arguments, so it was not re-usable at all
A better (IMO) solution would be to have a function generating the HTML code, and printing the result:
def ulify(elements):
string = "<ul>\n"
for s in elements:
string += "<li>" + str(s) + "</li>\n"
string += "</ul>"
return string
print(ulify(['thing', 'other_thing']))
You can also read about list comprehensions. It would make working with lists simpler:
def ulify(elements):
string = "<ul>\n"
string += "\n".join(["<li>" + str(s) + "</li>" for s in elements])
string += "\n</ul>"
return string
Solution 2
Looks like you are trying to build a website. Why don't you use a template engine, like Jinja 2 for this, instead of printing a HTML snippet from a function? For that you will need a Python web application, plausibly written in one of web frameworks. I'd go for Flask here, it's simple to start working with and Jinja is a default template engine for Flask.
If you just want to generate static HTML files, I would recommend Frozen-Flask, which allows you to generate static HTML files that can be hosted without a need to deploy any Python web application on your server. Just copy generated files to your hosting and you are ready to go.
If you still want to just print a HTML snippet, your code should be something like Ealhad posted in his answer.
Also, you original code contains a few problems:
def pizzatoppings(self):
# you don't need semicolons in Python
toppings = ['mushrooms', 'peppers', 'pepparoni', 'steak', 'walnuts', 'goat cheese', 'eggplant', 'garlic sauce']
# you need to initialize a "ul" variable
ul = "<ul>"
for s in toppings:
ul += "<li>"+str(s)+"</li>"
# following two lines where indented too much. In Python, indentation defines a block of code
ul += "</ul>"
return ul
Calvin Ellington
Python/Go Programmer and Sysadmin. Aspiring musician / creatively activated individual.
Updated on June 27, 2022Comments
-
Calvin Ellington almost 2 years
I'm attempting to write a function with python that will take an list of strings that I have defined and display them as a single string that is and HTML unordered list of said strings. So far my code is:
def pizzatoppings(self): toppings = ['mushrooms', 'peppers', 'pepparoni', 'steak', 'walnuts', 'goat cheese', 'eggplant', 'garlic sauce']; for s in toppings: ul += "<li>"+str(s)+"</li>" ul += "</ul>" return ul
When I attempt to run this however I get no traceback and nothing happens. Does anyone know why this is happening? I know this is probably a trivial question but I've searched for answers and cannot find a solution. Thanks!
-
Calvin Ellington about 7 yearsI can't believe I was forgetting to call the function, thanks for your help I've marked your answer as correct.
-
Ealhad about 7 years@NoOrangeJuice I'm glad! I can help you improve it a bit if you want ;)
-
Calvin Ellington about 7 yearsUsing a function to generate the code looks much simpler actually, thanks again!
-
Ealhad about 7 yearsIt's simpler indeed, and much more composable. You can use the generated text in other functions, or even print to a file instead of you terminal. Functions are good, use them! (and especially those which are already built-in!)
-
Calvin Ellington about 7 yearsI'm actually more familiar with template engines, and I agree they are much simple to use. However I'm trying to write the html snippet as a proof of concept kind of thing.