More with 'for'


#1

For some reason my code just hangs. now sure what i’m doing wrong. if i add anything after this it just hangs.

Write a for-loop that iterates over start_list and .append()s each number squared (x ** 2) to square_list.

Then sort square_list!

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

# Your code here!
for numbers in start_list:
  start_list.append(numbers ** 2)

#2

you are looping over and appending to the same list, this creates an infinity loop given the list keeps expanding


#3

Ok so i’m not understanding what I’m doing, or what .append is supposed to do.
I’m completely new to coding and python so this is hard for me.


#4

https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

append appends an item to the end of a list

if you add an item at the end of a list, and you are looping over this list, the loop will never reach the end, given you keep adding items to it


#5

Ok i think i get it after some reading.
im stuck now with this error

Traceback (most recent call last):
File “python”, line 8, in
AttributeError: ‘int’ object has no attribute ‘sort’

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

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

#6

Finally figured it out.
I did not need another “for” loop to sort the list.

Final code

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

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

square_list.sort()
print square_list

#7