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

Jamal Scott
Jamal Scott
9,656 Points

I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in item

I need you to help me finish my loopy function. Inside of the function, I need a for loop that prints each thing in items. But, if the current thing is the string "STOP", break the loop.

Cant seem to get this one.

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

It can be frustrating sometimes when you can't get past the challenge. I did the conditional first and THEN print(thing). Hope that helps!

11 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

Try this.

def loopy(items):
    for i in items:       #  Start loop assigning each item to i
        if i == "STOP":   #  Check to see if i is "STOP"
            break         #  If it is, break out of the loop
        else:             #  Otherwise...
            print(i)      #  Print i
Jeremy Kerrigan
Jeremy Kerrigan
12,002 Points

Don't forget white space matters.

Patric Daniel Pförtner
Patric Daniel Pförtner
1,542 Points

Hi Jamal,

You are very close to the answer, here is how I did it:

def loopy(items):
    # Code goes here

    for loopy in items:
        print(items)

Why are you learning Python? Maybe we have the same aims! I am learning it to bring my Start UP www.wolf-gate.com to the next level. Thank´s to Kenneth Love it´s easily possible :)

Endrin Tushe
Endrin Tushe
4,591 Points

Yep. Definitely helps to think of the items argument being a list rather than a single string.

wafic fahme
wafic fahme
3,309 Points
def loopy(items):
    for things in items:
        print (things)
        if things == 'STOP':
            break
wafic fahme
wafic fahme
3,309 Points

What is wrong here?

Endrin Tushe
Endrin Tushe
4,591 Points

Even though this is the correct answer. Could someone explain why the loop doesn't actually break when you enter the string STOP in the function. After the function has been defined and run, when you actually type in loopy("STOP") the for loop doesn't actually break, instead it runs through each of the elements in the string and prints them out as:

S T O P

This is the same as what happens when you enter in another word for items such as loopy("apples") you get: a p p l e s

Shouldn't STOP completely break the for loop instead of printing out the elements of STOP?

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



list = ['apples', 'pears', 'STOP', 'peaches', 'beer']

loopy(list)

So I made a list to give you a better idea of what the loopy function is doing. Then I called loopy with my list as an argument. Because list has 'STOP' in it, loopy will break. If you were to run this in python, you would see it only prints apples, and pears before breaking the loop. Does that help?

not sure if this is correct Python language, but this is what solved the first part of the challenge for me:

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

Matthew Costigan
Matthew Costigan
18,319 Points

def loopy(items): # Code goes here for item in items: print(items)

Afloarei Andrei
Afloarei Andrei
5,163 Points

I'm stuck. I tried this

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

and this

def loopy(items): for thing in items: print thing

nothing works.

Amekha Sritaboot
Amekha Sritaboot
795 Points

def loopy(items): for loopy in items: print(items)

Altavious Griffin
PLUS
Altavious Griffin
Courses Plus Student 2,598 Points

def loopy(items): [next line and do indent] for item in items: [next line and do indent] print(item)

Give it a try!! Be sure to Reply.