More with 'for'


#1



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


Oops, try again. It looks like square_list has x ** 2 at index 0 instead of 1.


The code should square and sort the numbers but instead it returns, 'x ** 2' over and over again


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

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


print square_list


#2

I don't believe you need that in quotes as a string but just x ** 2 to actually perform x ** 2


#3

I just tried that and it returns an error message that says that name 'x' is not defined.


#4

try number ** 2 instead of x ** 2 or numbers ** 2 whichever works


#5

Now it says that square_list has 49 at index zero instead of one.
This is so confusing!


#6

repost your code as it is now?


#7

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

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

print square_list


#8

It returns [ 49, 49, 49 ...]


#9

I figured it out! I just needed to add an s to the end of numbers on line 5!
Thank you for your help!


#10

I do not see much that could be wrong. However, what happens when you get rid of the s on numbers to make it just for number in start_list:


#11

Haha I think we came to somewhat the same conclusion! lol Glad to have helped! Happy coding!


#12

Hi can you tell me
why the code need to be ?
square_list not start_list in this one ?

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

As I try square_list it work out, but start_list it tells me infinite loop ?


#13

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