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 trialOscar Chong
Courses Plus Student 292 PointsPls help me out
Here is the question:
Same idea as the last one. My loopy function needs to skip an item this time, though.
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".
Can you tell me what is wrong with my code and how can i correct it? Thanks
def loopy(items):
# Code goes here
while True:
if items[0] = a:
continue
2 Answers
Bob Pickles
1,363 PointsThere's quite a few things wrong with this. I'll explain but I suggest you go back and watch some of the earlier videos as well.
Your code:
def loopy(items):
# Code goes here
while True:
if items[0] = a:
continue
I am going to deconstruct this for you and show you where the problems are:
while True should not be used here. The reason why is
- You have an infinite loop
- You are looking for the string element inside the list which you'll never get with while True.
- The proper way to do this is
for item in items:
as it will separate each member out into its own string
if items[0] = a: You have 2 obvious syntax errors here.
- The first is = (assignment) vs == (equality).
- The second is a isn't a variable, it's a string, you gotta put "" (quotes) around it.
You continue but then what? You don't say what to do if it actually finds something that doesn't start with "a".
Empowered with this new information, go back and see if you can figure out how to fix this. If you can't, come on back with your new code revisions and we'll work through it further.
lemelleio
10,874 PointsBob Pickles has got you covered.
Just want to help out with a visual representation of his breakdown:
#your code
def loopy(items):
# Okay, we're defining a function called loopy that takes the argument 'items'. 'items' is going to be a list: items = ["abc", "def", "hij"]
while True:
#You're going to want to iterate over that list her using a 'for' loop, that way the process terminates when its completed.
if items[0] = a:
#Just like bob said, if your code was right up to this point you would be checking the first index of the list 'items' is set to
#the variable a. That's definitely not what the challenge is asking. It wants you to check to see if the first index of the item ["[a]bc", ...] in the list of items is '==' equal to the string 'a'.
continue
#The challenge also wants you to reveal the item in items that does not meet your if statement after continuing.```