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 Continue

Dennis Moriarity
PLUS
Dennis Moriarity
Courses Plus Student 940 Points

Not clear on what to do

How do you exclude on char or position?

breaks.py
def loopy(items):
    # Code goes here

3 Answers

andren
andren
28,520 Points

You do it in pretty much the same way you did it in the break challenge. Strings are basically just a list of chars in Python. So you can pull out individual chars by using bracket notation, just like you can in a regular list.

So for example if I wanted to test if the first letter of a string was "H" the code would look like this:

example = "Hello, World!"
if example[0] == 'H':
    print("The first letter is H")

As for making the loop skip an item, that is done with the continue statement. It works similarely to break except that instead of ending the whole loop it just ends the current run of the loop, and starts on the next item.

Dennis Moriarity
PLUS
Dennis Moriarity
Courses Plus Student 940 Points

Still not understanding.

Here is the challenge. Loop through each item in items again. If the character at index 0 of the current item is the letter "a", continue to the next one. Otherwise, print out the current member. Example: ["abc", "xyz"] will just print "xyz".

Here is what i have so far.

def loopy(items): for item in items: if item[0] == "a' continue

andren
andren
28,520 Points

That's pretty much correct. But you have started the string with a double quote and ended it with a single quote which is invalid. And you have forgotten to include a : colon after the if statement.

If you fix those issues like this:

def loopy(items): 
    for item in items:
        if item[0] == "a":
            continue

Then you just need to place a print(item) statement below the if statement and your code will work.