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

I keep getting the error saying my "code took too long to run" on this particular exercise.

I'm still trying to learn the basic concepts and I thought I had a pretty good overview of how the breaks work but when I added the "while True:" statement, it's giving me the "took to long error". I tried to follow the example in the previous video and can't seem to get it right. I've tried the "if items..." statement without the "while True:" statement and that doesn't work. I'm not sure where my error is as this is how the example is set up.

breaks.py
def loopy(items):
    while True:
        if items == "STOP":
            break

    print (items)
    # Code goes here

2 Answers

Abdullah Alosaimi
PLUS
Abdullah Alosaimi
Courses Plus Student 2,280 Points

You are comparing the whole list against a single string 'DONE'.

Also, using an infinite loop is a bad idea. Here is a better way to do it.

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

Good luck Isaac.

Abdullah, Thanks! That helped a lot.

Trevor Tosi
Trevor Tosi
3,465 Points

The biggest problem you have is you are never looping through the "items" list. Therefore the line: if items == "STOP": will never be true because the value of the list will never be "STOP". You need to iterate through the list and compare each element not the entire list itself

Trevor, I'm new to this so I had to go back and look through some of the videos but this made a lot of sense once I realized what you meant. Thanks.