I'm getting a syntax error where I believe my syntax is correct


#1

I’m really confused.

If I have

def median(numlist):
  sorted(numlist)
  print numlist
  
median([1,2,3,4,5,6,7,8,9,10])

It all works out, but the second I do anything with the numlist, such as…

def median(numlist):
  sorted(numlist)
  if len(numlist) % 2 == 0:
    print "It's even"
    #Note: (len(numlist) / 2) should give me one integer to the right of the halfway point
  
median([1,2,3,4,5,6,7,8,9,10])

And THIS returns

  File "python", line 10
    median([1,2,3,4,5,6,7,8,9,10])
         ^
SyntaxError: invalid syntax

However, the line median([1,2,3,4,5,6,7,8,9,10]) is the exact same in the two cases. Is my syntax really incorrect if it worked with the barebones test above?


#2

The code you posted (middle code box) is syntactically valid:


#3

Do you know why it isn’t running on codecademy then? Should I report it as a bug?

Edit: OH

I am getting the error only when this line is in the code

print numlist[((len(numlist) / 2) - 1)

I see where I went wrong here, but I wonder why python pointed below this and said the syntax was invalid?


#4

You could have written other things that involved closing off the braces. It pointed where things stopped making sense, not at the mistake, python has no idea what you meant.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.