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 (2015) Shopping List App Continue

George Lugo
seal-mask
.a{fill-rule:evenodd;}techdegree
George Lugo
Python Web Development Techdegree Student 922 Points

What am i doing wrong for this problem

def loopy(items): if item in items: item[0]="a" continue else: print item

breaks.py
def loopy(items):
    if item in items:
        item[0]="a"
        continue
    else:
         print item

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The check for the first character needs to be an if-statement:

        if item[0] == "a":
  • "if" should be replace by "for" in the first statement to create the loop, with the above 'if` inside the loop
  • Be sure to indent the continue and else block to align properly with the new if
  • print() needs parens around arguments

Edit: updated to add for loop comment

George Lugo also needs to use a for loop :)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Thanks. Must have dropped that line when listing my changes from memory. I try not to post the completely corrected code or the full solution to encourage deeper learning. Post updated.

def loopy(iterable):
    for item in iterable:
        if item[0] == 'a':
            continue
        else:
            print(item)

How this works:

  1. We loop through every single element in the list. You forgot to do this :)
  2. If the first letter in the item (we retrieve the first item by using item[0] because Python starts counting at 0) is the letter "a", we continue or "jump" to the next iteration.
  3. if the item's first letter is not "a", Python goes into the else clause and Python prints the item. Remember to use parentheses () around item because print is also a function as well!

I hope this helps. ~Alex