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 trialcxpkuxlvmo
4,022 PointsFirst Challenge Question, Shopping List 2
Can anyone tell me how to skip if the index of 0 is "a"?
def loopy(items):
# Code goes here
for thing in items:
if thing[0] == "a":
thing.next()
continue
else
print(thing)
3 Answers
Kourosh Raeen
23,733 PointsHi Kristin - In the if section all you need is continue
, so delete the line thing.next()
. Also, you are missing the colon after else.
Ross Kopelman
17,523 Pointscan I use the command if thing.index(0) == "a": continue
else: print(thing) is this correct? Or should I use
if str(thing.index(0)) == "a" ??
Kourosh Raeen
23,733 PointsHi Ross - The index()
function does not return the character at a given index. It does the exact opposite, which is given a character or string it will return the index at which that character or string first occurs. For instance, if I have defined the following string:
my_str = "Treehouse"
then my_str.index('e') will return 2, which is the index of the first occurrence of 'e' in "Treehouse". Now, if you try my_str.index('eh') you will get back 3 since the substring 'eh' starts at index 3.
Ross Kopelman
17,523 PointsSo, for the given code challenge, would the following code work?
if thing.index("a") == 0: continue
Kourosh Raeen
23,733 PointsNo it wouldn't because that line throws a ValueError if there is no 'a' in the string. But it will work if you use the find()
method. It works like index()
but it returns -1 if the character or substring is not found.