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 trialDennis Moriarity
Courses Plus Student 940 PointsNot clear on what to do
How do you exclude on char or position?
def loopy(items):
# Code goes here
3 Answers
andren
28,558 PointsYou 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
Courses Plus Student 940 PointsStill 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
28,558 PointsThat'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.
Dennis Moriarity
Courses Plus Student 940 PointsThanks Andren, i missed that quote mistake. It works now.