More with 'for' #9


#1

I am a little stuck with part 9 in List and Dictionaries. The error message I got the first time was "Check your code for infinite loops" , That is why I put the break in the code. After I did that, I saved the code and the error message read, Oops, try again. Make sure not to modify start_list!

I'm not really sure what that means and if the 'break' has anything to do with it but any help is always appreciated. Thanks!!

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

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


More with 'for'
#2

Hi morganw01,

The error message I got the first time was "Check your code for infinite loops"

Your code will run as long as there are numbers in start_list. At every iteration, you are inserting another number in the list. The loop therefore never ends and you have an infinite loop.

After I did that, I saved the code and the error message read, Oops, try again. Make sure not to modify start_list!

Yes, you have to modify square_list instead. Calling .sort/.appendonstart_list will modify it.

Thank you!


#4

instead of start_list
use square_list


#5

3 posts were split to a new topic: Oops try again! square list needs to be the same length as start list


#11

Hi,

I'm having the same problems with my code too. I don't understand what I'm doing wrong.. could you help me?


#12

Please post a new topic with your code. Thanks!


#13

Actually, I figured it out via someone else's post.


#15

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

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

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

I dont understand the error. can someone help


#17

Hint: .append function does not return anything. So, doing:

square_list = square_list.append(numbers ** 2)

is wrong. You should not (and in fact can not) assign the result of calling .append because it does not return anything.


#18

Disregard, i have it sorted out


#19

What is wrong with my code?

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

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

#20

Hint: Incorrect indentation inside for loop.


#21

Hi I used the below code without break and its working fine and we have to use square_list in for :

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

As per my understanding this is the correct code


#22

While the code may pass the exercise, it makes more sense to sort the list after we have filled up the numbers, and not at every iteration of the loop :slight_smile: