-2

My function at the moment looks like this:

def list_of_char(my_list: list):
    n = "#"
    my_new_list = [i*n for i in my_list]
    return my_new_list

if __name__ == "__main__":
    print(list_of_char([2,3]))

It prints out:

['##', '###'] 

but my output must be like this:

##
###
Denis
  • 33
  • 5
  • 2
    ``print('\n'.join(list_of_stars([2,3])))`` – jak123 Nov 20 '21 at 19:02
  • The function works as it should but my test doesn't accept it and show me this: Calling list_of_char should not return anything, now it returns value which type is – Denis Nov 20 '21 at 19:20
  • What the problem it can be? It doesn't work if I remove 'return' – Denis Nov 20 '21 at 19:24
  • Does this answer your question? [Printing list elements on separate lines in Python](https://stackoverflow.com/questions/6167731/printing-list-elements-on-separate-lines-in-python) – wjandrea Nov 20 '21 at 19:30
  • 1
    @Denis For that, *"Calling `list_of_char` should not return anything"*, see [What is the formal difference between "print" and "return"?](https://stackoverflow.com/q/7664779/4518341) – wjandrea Nov 20 '21 at 19:34

1 Answers1

1

Your list_of_char() func returns a list, which you then print out. So you're seeing exactly what you're asking for.

If you want to print out strings, you need to either create the string in list_of_char() and return the string instead of a list. Or, you can take the returned list and print the elements of that list one at a time. Or you can convert the list of strings into a single string and print that out.

In the func you could change:

return my_new_list

to:

return '\n'.join(my_new_list)

which then would have the func return a single string.

In the alternative, you could change "main" from:

print(list_of_char([2,3]))

to:

print('\n'.join(list_of_char([2,3])))

Or, you could handle the list items one at a time (which now means not adding the extra newline (because you'll get one by default from print():

for line in list_of_char([2,3]):
    print(line)
Gary02127
  • 4,543
  • 1
  • 22
  • 27