Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Shopping List App Shopping List Introduction

Can't figure out how to use break properly on coding challenge in python

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

I've tried replacing word with items and moving the if statement to the same indentation as for.

Also how do I copy and paste code every time I try it just get condensed into one line

1 Answer

Brandon Oakes
Brandon Oakes
Python Web Development Techdegree Student 11,501 Points

So it looks like your function is taking one argument(items), which I am assuming is a list. Then you have a for loop that is taking each element(word) in the list and is printing that element(word). Why don't you incorporate a if/else statement. You just have a if statement.

for word in items: if word == "STOP": break else: print(word)

the if/else statements will allow you to tell python "hey do this IF this is true, ELSE always do something else". Also in your code, you are looping through a list to look at WORD but you are telling python to break if ITEMS== "STOP".