More with 'for'


#1

When I run the function, it tells me that the function is taking too long to reply and that there might be an infinite loop. Where did I make my mistake?


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

Hi this line

start_list.append(number ** 2)

you are putting the square of your number in that start_list instead of square_list


#3

I saw my mistake and fixed it but now it tells me that x is not defined.


#4

can you post your new code?


#5

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

Your code here!

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

print square_list


#6

wait I figured out my mistake. Thanks for all your help and advice. Sorry for the inconvenience.


#7

Hi, I have the same problem. Can you tell the reason please? why it says x is not defined?thanks


#8

Can you show me your code?


#9

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

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

print square_list


#10

The error is
AttributeError: 'NoneType' object has no attribute 'append'

Thanks


#11

Try this:

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

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

print square_list


#12

Does not work as well. I just solved this by using ==
square_list ==square_list.append(number**2)
Thanks very much for your help!


#13

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

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

print (square_list)

This works too. with square_list between ()


#14

that code works fine for me:

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


#15

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

Your code here!

for number in square_list:
square_list.append(number ** 2)
square_list.sort()
print square_list
Its not working!
can you help?


#16

Hi this line

for number in square_list:

it should be start_list instead of square_list.


#17

hello! I have modified my code but its showing me the same error
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


#18

you need to indent everything after for number in start_list


#19

I just created a Variable and then put it all together.
root = number ** 2 ### created the function and then I appended that function to the square_list = [ ] etc...

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

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

print square_list.sort()


#20

So at first I had to copy the code from this string, because once again I felt that the instructions were not clear. When I ran your code, I got the same time-out error. After looking at your code and getting a better understanding what the instructions were really asking, I came up with:

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

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

print square_list