More with 'for'


#1




I'm not sure what i'm needing to do to complete the course correctly. I feel like I have produced an outcome that should be acceptable, but cant get a green response. I'm curious of what code academy is recommending I do instead.

Here is the error I get:
Oops, try again. It looks like square_list has 2 at index 1 instead of 4.


Here is exactly what my code produces in the window to the right:

"
Start
5
3
1
2
4
Squared and ordered
1
4
9
16
25
None
"


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

print "Start"
for start in start_list:
    print start
    square_list.append(start)
    square_list.sort()

print "Squared and ordered"
for square in square_list:
    print square ** 2


#2

The values being appended to square_list need to be squared at time of appending.

.append(start ** 2)

Don't sort until after the loop (remove indent). Now the square_list can be printed without doing any more math.


#3

So it's kind of a preference of where you want to do your math. Looking at what I wrote now there appears to be no use for the new "square_list". I could just do two "for start in start_list" statements, one with and one without sort, to get my result. Doh

Thanks for the help.


#5

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