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

maike willuweit
maike willuweit
552 Points

my break command is not working for a list

I am trying to add a break into the easy script, but I'm failing to see what I did wrong...

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

2 Answers

Two problems:

  1. The if statement to Python is outside of the for loop, you should move in the condition by adding an extra 4 spaces to the last two lines of code.
  2. This is very confusing, but the if condition has to be before the print() line of code. If we pretend to run our code, the result will be (we are calling the function with the argument [1, 2, 3, 4, 5, "STOP"]):
1
2
3
4
5
"STOP"

However, the code challenge isn't expecting you to print "STOP" at the end.

To fix that, you must move the if condition above the print statement.

Answer:

def loopy(items):
    # Code goes here
    for item in items:
        if item == "STOP":
            break
        print(item)

Good luck! ~Alex

maike willuweit
maike willuweit
552 Points

thanks for the nice explanation! It works :-)!

No problem :)