More with 'for'


#1


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

-I thought I had everything right, but I keep get the error message on line 6, with the code: square_list.sort()
AttributeError: 'NoneType' object has no attribute 'sort'

Thanks for the help!

Error message:
Traceback (most recent call last):
File "python", line 7, in
AttributeError: 'NoneType' object has no attribute 'sort'


1. start_list =  [5,3,1,2,4]
2. square_list =[ ]
3.# Your code here!
4. for number in start_list:
5.     square_list= start_list.append(number**2)
6.     square_list.sort()
7. print square_list


#2

append will update the list for you, no need to also store it in a variable

are you certain you want to sort the list each iteration of the loop, not just at the end?


#3

I want to loop at the end, and I thought that how the code should look like. I'm sorry but I thought my original code square_list.sort() will sort the list at the end, not each iteration.

I'm curious; after I created the square_list and want to make change to the list item by dividing every item in the square_list by 2.

here is my code:

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

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

print square_list

But the console produce error message. Below is the link to my code:
https://www.codecademy.com/en/courses/python-beginner-en-pwmb1/1/6?curriculum_id=4f89dab3d788890003000096

Thanks very much for your help.


#4

you appending to the list you are looping over, this causes an infinity loop

please just do what the exercise tell you, otherwise go play in the codecademy labs


#5

Hey mate,

I just finished the exercise and I think that you just have to pay attention to line four and how this relates to line 5. As stetim94 said 'append will update the list for you, no need to also store it in a variable'


#6

Your do not need to use start_list inside the for loop..square_list (number**2) will suffice and thats what the question demands..also sort must be outside the for loop before print.


#7

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