More with 'for'


#1

Here is what I have. I know im missing something but cant seem to figure it out. Any advice would be much appreciated.

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

# Your code here!
for square_list in start_list:
  square_list ** 2
  
  
print square_list


#result given = 4

#2

can you share the exercise url? Not sure what the instructions are

this is risky:

for square_list in start_list:

now the loop iterator (square_list) will overwrite the list you defined with this name earlier. So why would you use square_list here?

then inside the loop, you do square list to the power 2, but nothing with the result of this math

then you print square_list after the loop, which will contain last value of list


#3

Thank you for the reply. I ended up playing around with it and realized I needed to create an entirely new variable. I ended up passing the lesson with this:

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

Your code here!

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

print square_list

Which gave this answer: [1, 4, 9, 16, 25]


#4

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