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 trialDanielle Murray
Python Development Techdegree Graduate 7,755 PointsHi could you please help me get the right output for this task, I'm getting a syntax error on my first line
What am i doing wrong for this code? Please can you urgently help me with this task. Thank you.
def pop([1,2,3], 1):
pop.remove(1)
return pop
pop.remove(2)
return pop
pop.remove(100)
return pop
3 Answers
Mel Rumsey
Treehouse ModeratorHi Danielle Murray! There are a few things in your code that aren't quite right. So first we want to make a function that takes 2 parameters. [1, 2, 3], 1
are just examples. We can simply do def pop(lst, ind):
because it will be taking in any list and any index. Any code that we want the function to perform needs to be inside of the function.
def pop(lst, ind):
<code goes here>
We want the function first try
to pop a value from a list at the index. lst.pop(ind).
If the index is out of range this is going to raise an IndexError
. So we want an except
here that excludes index errors and would return
the string Invalid Index
.
Else
we can return the list.
Hope this helps!
Mel Rumsey
Treehouse Moderatordef pop(1st, ind):
# missing try
1st.pop(ind)
except IndexError:
return Invalid Index
Else:
return pop
^ I went ahead and wrapped your code in some code brackets so we could see it formatted. You are much closer here, but there are still a few things to adjust.
- You currently have a parameter that starts with a number. This is throwing a syntax error. you might want to change the 1 to an l (lowercase L) as the shortened version of list since list is a Python keyword that cannot be used.
- You are missing your
try
which should be right after the function definition - The except should return a string
- Else should not be capitalized
- You are currently returning
pop
rather than the list. We want the list parameter to be what is returned in the end.
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsHi Mel Rumsey my first line is still giving an error
def pop(1st, ind): try: 1st.pop(ind) except IndexError: return "Invalid Index" else: return 1st.pop(ind)
Mel Rumsey
Treehouse ModeratorDanielle Murray It looks like you still have the number 1 at the beginning of a variable which is going to throw an error. Try changing 1st to say lst or my_list
or anything else that does not have a number at the beginning of the variable. This will cause a syntax error.
Danielle Murray
Python Development Techdegree Graduate 7,755 PointsDanielle Murray
Python Development Techdegree Graduate 7,755 PointsHi Mel Rumsey this is my revised code
def pop(1st, ind): 1st.pop(ind) except IndexError: return Invalid Index Else: return pop
But i am still getting a syntax error on the first line. Please could you help me.