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

Daniel Petrov
Daniel Petrov
3,495 Points

For loop

A bit confused about this one: why would we use a "for loop" where here a while loop should be used?

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

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are very close. The if statement needs indenting to match the print to become part of the for loop. Also the if and print statements should be reversed so the break can happen before the print or the "STOP" will be printed.

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

Edit added completed answer

why do i not need " else:" after the break?

For loops are more common.

While loops can go off the rails if you never hit your stopping condition, or you might never use your while loop if the start condition isn't true when it reaches that line.

You can almost always figure out how many times your for loop will run just by looking at the code. From your code, I can see that the maximum number of iterations is the length of the list items. Depending on how you set your while loop condition I might not know that just by looking.

Daniel Petrov
Daniel Petrov
3,495 Points

Thanks Evan, that all comes with the experience I suppose.

Daniel Petrov
Daniel Petrov
3,495 Points

Thanks Chris, it makes sense but it still says: Bummer! Didn't find the right items being printed.

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

Not sure if you can see the code with the right indentation ... How can I post directly from the challenge black board?

[MOD: added ```python markdown formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Do not reset the value of items in the 3rd line. It clears the argument passed to the function.

def loopy(items):
    # Code goes here
    ### items = []  # <-- remove this line
    for item in items:
        if item == "STOP":
            break
        print(item)  # <-- remove 's'
Daniel Petrov
Daniel Petrov
3,495 Points

Ah, still the same message. There must be something wrong as I already tried without that line. Reloaded the page as well but no success ...

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Yes. There was typo in the print statement. Should be "item", not "items". Answers corrected.