Syntax Error When Including an Integer in Function Parameter

This is for Python Loops 5. Odd Indices, but it may be applicable to other challenges.

When I run the code:

#Write your function here

def odd_indices(1st):

new_1st =

for i in range(1, len(1st), 2):

new_1st.append(1st[i])

return new_1st

#Uncomment the line below when your function is done

print(odd_indices([4, 3, 7, 10, 11, -2]))

The output is:

File “script.py”, line 2
def odd_indices(1st):
^
SyntaxError: invalid syntax

However, if I simply delete the ‘1’ out of ‘1st’ to make it ‘st’, the problem clears and the code works.

Example:

#Write your function here

def odd_indices(st):

new_1st =

for i in range(1, len(st), 2):

new_1st.append(st[i])

return new_1st

#Uncomment the line below when your function is done

print(odd_indices([4, 3, 7, 10, 11, -2]))

Output:

[3, 10, -2]

My question is why is this happening?

p.s.

I am new to programming and this is my first post. Apologies in advance if my formatting is not great or if I have missed something obvious.

Variable names cannot start with a number. It is common when a function has a parameter that will be assigned to a list to name that parameter lst. That is a lower case L not the number 1. :wink:

2 Likes

Thank you for explaining this to me! :smile:

1 Like