Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using .split() and .join() in Python

I am currently learning some Python in Treehouse, but I came upon this challenge and do not know what I am doing wrong. The challenge is split into three parts, shown below with the prompt and the code I have written. I seem to be making the mistake in the third part.

Part 1:

I think it's time for a snack. Luckily I have a string full of types of ice cream sundaes. Unluckily, they're all in one string and the string has semi-colons in it too. Use .split() to break the available string apart on the semi-colons (;). Assign this to a new variable sundaes.

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")

Part 2:

Let's add a new string variable for displaying our menu items. Make a new variable named menu that's set to "Our available flavors are: {}.".

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")
menu = "Our available flavors are: {}."

Part 3:

Alright, let's finish making our menu. Combine the sundaes list into a new variable named display_menu, where each item in the list is rejoined together by a comma and a space (", "). Then reassign the menu variable to use the existing variable and .format() to replace the placeholder with the new string in display_menu. If you're really brave, you can even accomplish this all on the same line where menu is currently being set.

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")
display_menu = sundaes.join(", ")
menu = "Our available flavors are: {}.".format(display_menu)

Whenever I run the third part, Treehouse reads, "It looks like Task 1 is no longer passing," but when I ran the first part by itself, Treehouse accepted it.

Thanks.

like image 243
Alex Avatar asked Dec 05 '25 09:12

Alex


1 Answers

You should use

display_menu = ", ".join(sundaes)

sundaes is a list and it doesn't have .join, you can check by opening a python interpreter and running :

>>> dir(list)

but the string object hast .join

>>> dir(str)

and by running

>>> help(str.join)

we can see the description Help on method_descriptor:

join(...)
S.join(iterable) -> string

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.
like image 181
Matei Avatar answered Dec 07 '25 00:12

Matei