Why doesn't my code work?


I ran the following code as you mentioned in the lesson:

def takes_many_args(*args):

long_list_of_args = [145, “Mexico City”, 10.9, “85C”]

But threw an error:
TypeError: sequence item 0: expected str instance, int found

“145,Mexico City,10.9,85C”

I’ve had put quotes in the numbers in the list and it works.
long_list_of_args = [“145”, “Mexico City”, “10.9,” “85C”]

I’m not sure whether I’m missing something or misunderstanding a specific concept…

1 Like

First, please post code in readable form using the </> icon at the middle of the menu bat at the top of the text box you are typing in.

The string method str.join(iter) where iter is an iterable (e.g., a list) will fail if any member of iter is not a string.

1 Like
long_list_of_args = [145, "Mexico City", 10.9, "85C"]

def takes_many_args(*args):
  str_args = [str(arg) for arg in args]
  print(', '.join(str_args))

1 Like