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

Challenge 2 of 3 in lists

Hi,

I have no clue on how to complete this challenge:

Create a variable named greeting_list that's the string "Hi, I'm X" split on the spaces.

6 Answers

Here is what I got.

I actually got stuck on this one as well. The questions wasn't worded very well in my opinion & I had trouble with Task 1 not passing once I got to Task 3.

I think what he meant when he referred to 'X' was actually the '{}' place holder for referencing variables to inject at run time.

full_name = input("What is your full name? ");
name_list = list(full_name.split());
greeting_list = "Hi, I'm {}".format(name_list[0]).split();
greeting = " ".join(greeting_list) + ".";
print(greeting);
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You don't need input() since this isn't being run interactively. You should actually put your full name in the variable as a string.

And for the "Hi, I'm X" part, split that string on the spaces. That'll give you a list, which you should replace the last item of with the first item in your name_list variable.

Ultimately, you'll end up with greeting being "Hi, I'm Kenneth." (substitute your own name)

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Deep Dhanak

You can split a string in Python like this:

"Hi, I'm X".split()

You just need to store that in a variable named greeting_list

Hey Deep,

I am not sure if you have solved this challenge. I had some trouble as well, so I broke down my answer for you. The questions is asking you to take your name_list and and add it to the "Hi, I'm X", then split the entire string up into a list again. So, in the first challenge you made a variable with your full name like so:

full_name = "brent hicks"

Then you were requested to split up that variable into a string. This requires the use of str.split() :

name_list= str.split(full_name)

In this challenge we are taking the list in name_list and adding it to the string "Hi, I'm {}" and turning it into a new list. To do this, we need to format the "Hi, I'm {}" string to take the name_list. We have to pass in the values that are contained in the list with [0]. After, we need to surround the list with a str.split() to make the entire value produce a brand new list:

greeting_list = str.split(("Hi, I'm {}").format(name_list[0]))

I hope this help, if you have not already passed this with the help from the comments above.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Just want to point out that you can split a string directly, like "hello there".split(). You don't need to go to the object's type, str.

Mark Long
Mark Long
15,763 Points

This question makes no damn sense to me...

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Take the string "Hi, I'm X", and split it on the spaces so it becomes a list. How do you split up a string into a list?

Mark Long
Mark Long
15,763 Points

I played with it on IDLE and finally got it. The question wasn't very clear to me at first.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'd love any suggestions you have on how to clarify the question.

Lidija Novakovic
Lidija Novakovic
6,634 Points

You are not alone... English is not my native tounge but the formulation of the question is odd. One example - Create a variable named greeting_list containing the string "Hi, I'm X" and split the string on the whitespaces.

Samuel Cyrus
Samuel Cyrus
6,567 Points

thanks, was stuck on this section