More with 'for'

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?

```python

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

<do not remove the three backticks above>
1 Like

Hi this line

start_list.append(number ** 2)

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

2 Likes

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

can you post your new code?

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

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

1 Like

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

Can you show me your code?

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

The error is
AttributeError: ‘NoneType’ object has no attribute ‘append’

Thanks

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

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

2 Likes

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 ()

4 Likes

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

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?

Hi this line

for number in square_list:

it should be start_list instead of square_list.

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

1 Like

you need to indent everything after for number in start_list

1 Like

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()

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