0

I want to print the parameters as a list but I got this error. I know I can do this with other ways but I want to learn why this error occured. Here is the code:

def likes(*names):
    list=list(names)
    print(list)
likes("max","john","evan","matilda")
awyr_agored
  • 613
  • 3
  • 19
  • Change your variable name `list` to something else. – costaparas Dec 24 '20 at 11:44
  • change variable list to something else e.g. my_list. Get rid of asterisk (no need to get confused with pointer notation in Python). Put the list of words inside [ ]...def likes(names): my_list=list(names) print(my_list) likes(["max","john","evan","matilda"]) – awyr_agored Dec 24 '20 at 11:45

1 Answers1

0
def likes(names):
    my_list=list(names)
    print(my_list)

likes(["max","john","evan","matilda"])
  1. Change the function parameter by getting rid of '*' to just names.
  2. Change your 'list' variable to something else since the compiler could get confused. Never use a Python keyword as a variable name.
  3. The list() function takes an array parameter so you need to enclose the names in between [ ].
awyr_agored
  • 613
  • 3
  • 19
  • Please note the *only* actual problem with the original code is the use of `list` as a local variable name. There's no need to change the input to be a list -- the point of the function is to convert the arguments into a list & print it. Read more about packing & unpacking [here](https://stackoverflow.com/questions/45346116/how-are-pythons-unpacking-operators-and-used). – costaparas Dec 24 '20 at 23:16