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 Technical Interview Prep: Python Basics Basic Python Once You Pop

Danielle Murray
seal-mask
.a{fill-rule:evenodd;}techdegree
Danielle Murray
Python Development Techdegree Student 4,494 Points

Hi 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.

pop.py
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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

Hi 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!

Danielle Murray
seal-mask
.a{fill-rule:evenodd;}techdegree
Danielle Murray
Python Development Techdegree Student 4,494 Points

Hi 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.

Mel Rumsey
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

Hi Danielle Murray

def 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Danielle Murray
Python Development Techdegree Student 4,494 Points

Hi 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Mel Rumsey
Treehouse Staff

Danielle 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.