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 Python Basics (Retired) Shopping List Lists and Strings

Why does my code not work? Challenge Task 3 of 3 - Shopping List

I'm having trouble figuring out why my code is not passing. Any help would be greatly appreciated. Thanks.

Here is my code:

full_name = "Imer Pacheco"

name_list = full_name.split()

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

for item in greeting_list:
  if item == "X":
    print(name_list[0])

greeting = " ".join(greeting_list)

print(greeting)

4 Answers

Ah, okay. It looks like you have everything correct up till the the for loop. You can complete this challenge without the for loop.

full_name = "Imer Pacheco"

name_list = full_name.split()

greeting_list = "Hi, I'm X".split()
greeting_list[2] = name_list[0]
greeting = " ".join(greeting_list)
print(greeting)

No need to iterate through a loop. You can just replaces the in the list at the right index. (I tested it in Workspaces, and it does the trick. It should pass the challenge.)

Hi Imer. You are getting a syntax error due to improper indentation. Python is unusual in that the indentation is part of the syntax rather than just a convenience for readability's sake. Below is the correct indentation:

full_name = "Imer Pacheco"

name_list = full_name.split()

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

for item in greeting_list: 
  if item == "X": 
    print(name_list[0])

greeting = " ".join(greeting_list)

print(greeting)

The code still doesn't quite work, but this will remove the indentation errors. I can help you more if you need further assistance, but maybe this will be enough to get you on the right track.

Thank you for your response Tree. I'm afraid the way I cut and pasted my code in the browser was not an accurate representation of my code. I actually did have the exact indentation you suggested. Currently, the code evaluates to the following in Workspaces:

Imer

Hi, I'm X

I'm still not clear as to how to replace the "X" with my first name.

Tree, you are amazing! Thank you so much. Turned out to be a simpler solution than what I was doing. I appreciate your guidance :)

You're very welcome. Glad it worked!