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

I get a NameError: that says the 'loopy(items)' is not def.??

I am pretty sure I have quit a few errors in this code as well if someone would like to point me in the right direction I would very much appreciate it.

breaks.py
def loopy(items):
    # Code goes 
    while True:
        for item in items:
            print(item.next())
    while True:
        for item in items:
            if item.index('a') == 0:
                print(item.next())
            else:
                print(item)

1 Answer

andren
andren
28,558 Points

I think you might be over thinking this task, your code is far more complex than it needs to be. I think the best hint I can give you that you seem to have misunderstood based on your code, is that to skip an item in a for loop all you need to do is use the keyword "continue" like this

for item in items:
    if SomeCondition:
        continue
    else:
        print(item)

That would skip the item if "SomeCondition" was true.

You don't need to use something like the next method or nested loops or anything complex like that.

The other issue is you should not use the index method for this type of task, the index method raises an error if it cannot find the character you provide it in the string you ask it to check. A safer and easier way to check if a certain character is at the start of a string is to use bracket syntax like this:

item[0] == 'a':

That checks if the character at index 0 of the item string is 'a'.