Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Why can't I get this task to work - where I seem to just need to swap the first item of a list in Python?

Here is the code in the challenge: full_name = "Bruce Martin Whealton"

name_list = full_name.split()

greeting_list = "Hi, I'm Treehouse".split()

swap_val = greeting_list[2]

name_list = swap_val + name_list

First I create full_name and that works fine. Then I convert it to a list by using full_name.split() That works fine. The next step is to create a greeting list using split, which splits on spaces from "Hi, I'm Treehouse" This works fine. Now the third step is where the problem arises. I thought it was simple, but the quote simple unquote first try by me, which seemed to be straight forward and easy turned out to be wrong.

The instruction reads: "Now, swap the value of "Treehouse" in greeting_list to be the first item in your name_list variable."
Originally, I did this in the Python interpreter and got what I thought was the right answer.
I didn't use swap_val or that line at all. I just did

name_list[0] = greeting_list[2]

Then inside the python interpreter I entered name_list and I got ['Treehouse', 'Martin', 'Whealton']

So, now Treehouse is the first item in the name_list variable which points to the new name_list that has swapped Treehouse for 'Bruce' But when I check my answer, it says that Task 1 now fails. Task 1 is where I set full_name to my full name and then use split and assign the resulting list to name_list. Maybe it is as simple as making a new variable for the new list.
Thanks, Bruce

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Chris Freeman prompt has been updated. thanks for the suggestions.

1 Answer

The instruction reads: "Now, swap the value of "Treehouse" in greeting_list to be the first item in your name_list variable.

I think the instructions want you to do greeting_list[2] = name_list[0], not the other way around. It isn't worded very well, but that is how I read the instructions.

In the end: greeting_list = ["Hi,", "I'm", "Bruce"]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Tagging Kenneth Love

There are many similar forum questions that are misinterpreting this challenge task.

Now, swap the value of "Treehouse" in greeting_list to be the first item in your name_list variable.

Mainly due to the ambiguity of "swap... to be... in...", this is being read by some as changing name_list instead of changing greeting_list

Suggested alternatives:

Now, [replace] the value of "Treehouse" in greeting_list [with] the first item in your name_list variable.

Now, [change] the value of "Treehouse" in greeting_list to be the first item [from] your name_list variable.