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 Break

Jim Awofadeju
Jim Awofadeju
4,097 Points

I am having trouble with this code challenge. Please show me the correct code for this challenge.

I am having trouble printing every thing in items. I tried "print item" and "print items". I set up the for loop statement correctly. I set up the if statement correctly with a line to break out of the loop. I called the loopy function with arguments passed into it. Please show me what is either missing from my code or what is incorrect. Thank you.

breaks.py
def loopy(items):
  for item in items:
    print item
  if item == "STOP":
    break

loopy([1, 2, 3, 4, "STOP", 5, 6])
    # Code goes here

4 Answers

Paul Cox
Paul Cox
12,671 Points

A couple of things:

1) You need to break from the loop before you print the item if the item.

2) You need to indent your if statement correctly so it is part of the loop.

3) You need to use Python 3 print syntax ( e.g. print(item) )

def loopy(items):
  for item in items:
    if item == "STOP":
      break
    print(item)

loopy([1, 2, 3, 4, "STOP", 5, 6])
Jim Awofadeju
Jim Awofadeju
4,097 Points

I added a line of code called "break" after "print (item)" and before "if item == "STOP":" I followed the first thing you wrote and the third thing you wrote. I am having some problems with indentation. How many spaces should the for loop be indented, the first "break" line, the "if statement", and the second "break" line? Since a colon goes after the function definition and the if statement, Python automatically indents to the proper position when you press Enter or Return. Do I have that right?

Paul Cox
Paul Cox
12,671 Points

Yes, Python should automatically indent but you will need to remove the indent when you want to exit a code block.

The for loop should be indented 2 spaces, the if and print statements 4 spaces, and the break statement 6 spaces. Your if and break statements should come before the print statement.

Hopefully this helps and you're really close to the solution, but if you want more help I can post a solution.

Jim Awofadeju
Jim Awofadeju
4,097 Points

You helped me. Thank you!

Jim Awofadeju
Jim Awofadeju
4,097 Points

I want more help. Please post a solution.

Paul Cox
Paul Cox
12,671 Points

I've edited my original answer to include the solution. Hope this helps :)