Help Please


#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. The result I get back is correct but it asks me not to modify start_list. I'm not sure how to move forward. 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


More with 'for'
#2

Oh, I just noticed I've helped you before :slight_smile:. So, when looping, we need to create a new variable that will iterate through our list.
Example:

for new_variable in original_variable:
     #etc...

We dont need to define a variable like this in out loop : new_variable = #whatever
we can just do in in our loop :slight_smile:.


#3

I'm sorry, but I just don't understand. I'm a visual learner. Can you please show me in context how that might look so I can figure this out?


#4

So currently, we're looping through start_list. Where is that? it's the start_list after in. The start_list after for is the one that will loop through start_list = [5, 3, 1, 2, 4].
But this is bad because Codecademy doesn't want us to modify our list (start_list).

What should we do? Use something else like a new iterator to loop through start_list. Exapmle:

for new_iterator in my_list:
     #anything happens here...

new_iterator will hold the same values as start_list, the only difference is: new_iterator is not a list! :slight_smile:

Now... whats next in your code:

Since we don't want to modify our list (like I mentioned) we can use our new variable to square (** 2) and append to square_list.


#5

Thank you for your help! I finally have understanding and closure.


#6

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