More with "for"


#1



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


The program took too long to finish. Check your code for infinite loops and try again.


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

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

print square_list


#2

you are appending to the same list as you are looping over ,this give infinity loop


#3

Hi @nish147,

Here, you are building upon start_list when you should build upon square_list ...

    start_list.append(number ** 2)

#4

Thanks guys for your help :smile:


#6

hi guys can somebody explain why:

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

is NOT the same as:

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

much appreciated!


#7

** is the python operator for till the power (^), so:

3 **5 (which is 3 * 3 * 3 * 3 * 3 = 243)
5 ** 3 (which is 5 * 5 * 5 = 125)


#8

You need to .append(number ** 2) into square_list, not start_list. Because that gives the result of start_list = [5, 3, 1, 2, 4, 25, 9, 1, 4, 16]
You want to embed the squares of the numbers into square_list, that is what the task requires. And watch out for infinite loops: your loop is sorting the empty square_list, so the loop never starts and never finishes! Take away the whitespace


#9

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