My code won't work but another will


#1



Here's the link to the excercise I was working on - https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096


The errors I've been getting are "Invalid Syntax" for line 4 (AKA the for loop)

I wrote the code before going to the forums. I came here and found that I did the indentation wrong so I fixed that. Now what I can't figure out is why my code won't work when it's almost word for word same as this other one I found while looking around in the archives.

I don't know if I'm doing something wrong or what.

This is the code I wrote before I came to the forums to see what's up:

start_list = [5, 3, 1, 2, 4]
square_list = []

for x in start_list: # Your code here!
    square_list.append(x ** 2)
square_list.sort()
print square_list


This is the code I found in the forums that looks like, what I think I wrote too:

start_list = [5, 3, 1, 2, 4]
square_list = []

for number in start_list # Your code here!
    square_list.append(number ** 2)
square_list.sort()
print square_list

Thanks!


#2

I see only one problem and it is in the second snippet. The first snippet is perfect.

The second snippet throws error because Python expects a colon after the for statement:

for number in start_list # <-- right here
    square_list.append(number ** 2)

#3

Oh. Haha. The second code is mine becuase I know I forgot the colon. That makes a whole lot of sense. Thank you!


#4

You're very welcome :slight_smile:


#5

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