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 trialMark Kohner
1,121 Pointspython loopy break; not clear where I am going wrong on this! Break/print
finish loopy , print each item, break on string 'stop'
def loopy(items): for items in loopy: print(items) if items == 'STOP' break i am not sure about indentation in functions, but I have tried several other ways and do not get any feedback besides 'Bummer! Try again!' .
def loopy(items):
# Code goes here
for items in loopy:
print(items)
if items == 'STOP'
break
Abdallah El Kabbany
2,042 Pointsoh got it so what you are trying to say that it is going step by step right? in other means:
- check if it is stop
- if yes stop
- if no print the value
Right?
Grigorij Schleifer
10,365 PointsHey,
you are super right !!!
def loopy(items):
for item in items:
if item == "STOP": # check if it is stop
break # if yes stop
print(item) # if no print the value
Abdallah El Kabbany
2,042 Pointsthank you :)
2 Answers
Grigorij Schleifer
10,365 PointsHi Mark,
just move the if block with the break statement in the scope of the for loop.
def loopy(items):
for item in items:
if item == "STOP":
break
print(item)
Grigorij
Abdallah El Kabbany
2,042 Pointsbut why do i have to bring the print function below the if condition what is the problem if i have it above it?
Mark Kohner
1,121 Pointswhy do we use a new 'iterator' (item in items) instead of using the parameter for the function? I am confused on this.
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHey,
the print statement belongs to the for loop. So if the input is not STOP the items will be printed.
You have to check if it is STOP first and if yes break the code and only after this print your item.
Makes sense?