More with 'for'


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096#


I thought my code was producing the desired result...all numbers in start_list squared then sorted. I think I'm not understanding what the instructions are asking of me. Any help would be appreciated.

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

for start_list in start_list:
    square_list.append(start_list**2)

square_list.sort()
print square_list

#2

Since, we're trying to square each number in our list and append it to square_list, we need to create a variable that will be used to iterate through start_list in our for loop.


#3

I think I'm fundamentally misunderstanding something...
The result of my code (above) produces the result: [1, 4, 9, 16, 25]

Your response:

Maybe I'm just having one of those days, but isn't that what my code did?


#4

Yes, that's your result. It is the correct result.

Yes. I mean it's only like this when appending to non-lists.
(my apologies... in your code, it looked like you were appending to start_list, so that's why I said that. So ignore that.)

We are asked to not modify start_list, so we need to create a new variable that will iterate through start_list. :slight_smile: Do you know what I mean?


#5

I’m having the same issue - correct result but telling me to not modify start_list and I can’t figure out the solution


#6

Hello @bunn_cole. Check out this link:

If, you need more help, or if this doesn’t solve your problem, you can create a new topic like it’s advised in the Guidelines. Thanks :slight_smile:


#7

Perhaps you misunderstood my note - I am having the same problem and am looking for a solution.


#8

Do you mean by me giving you the full answer? I’m sorry, but that’s against the guidelines, so I can’t do that. Besides, you don’t really learn anything by just copy and pasting.


#9

please don’t modify start_list use another variable like A which is provided in hint:

start_list = [5, 3, 1, 2, 4]
square_list = []
for A in start_list:
square_list.append(A**2)
square_list.sort()
print square_list


#10

Thank you for giving a straight answer, i’ve been on this same lesson for 2 hours :tired_face:.
To my understanding the syntax for the for loop is :
“for <some_variable> in <some_list>:”

Is this the general idea of it?


#11

Guidelines? What are these Guidelines??? I might in some trouble…:expressionless:


#12

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