9.more with "for"


#1

1、Write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list.
2、Then sort square_list!
?

start_list = [5, 3, 1, 2, 4]
square_list = []
for square_list in start_list:
square_list ** 2
print square_list

what is wrong with this code??????who know the standardized answer??thank you


#2

here:

for square_list in start_list:

why would you use square_list as loop iterator? This will overwrite the empty list. Use something else, the loop iterator will contain all the values from start_list

then you need to .append() (you learned this function, right?) each value squared to squared_list

after the loop, sort the list, you learned functions for sorting, right?


#7

Thx
I thought for square_list in start_list: would store value of start_list to square_list…
am i right??


#8

no, the loop iterator only contains read only values from the list and are stored as integer (temporary) in the loop iterator.

You can’t store values in a list by using the list as loop iterator.


#9

A post was split to a new topic: What is wrong with this code?


#10

And the final code like this below:

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

is that right??


#11

yep, that is absolutely right :slight_smile: well done :slight_smile:


#12