More with 'for'


#1



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


Oops, try again. Make sure to sort square_list after filling it!
None

it should square all the numbers in the start_list 5**2=25, 3**2=9 and so on with the answers printed to the console?!!!

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

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

print square_list


#2

The list "square_list" should start out as an empty list, which you declare like this:

square_list = [ ]

Then you want to append the square of each item in start_list to square_list.
Check the first line underneath your "for" statement. Is that appending to square_list?


#3

I had the square_list empty but was throwing up a different error so i put the **2 in it.Oops, try again. square_list should be the same length as start_list is the error i get when empty.the second line under my for statement is the append ?!!! a bit confused


#4

this is what i have now, append list is under for statement. all indents are ok

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

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

print square_list


#5

Check this line:

for square in start_list

You can't use the same name for your function and for a variable inside the function....try changing this back to "number" like you had before.


#6
  1. error
    None
    Oops, try again. Your code caused the following error: object of type 'function' has no len()

i put len into after the error

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

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

print square_list

2.error Oops, try again. square_list should be the same length as start_list

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

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

print square_list

I starting to confuse my self sorry, i just cant figure it out have try a few other ways, no good.
could you explain again what im doing wrong.Functions is called "start_list" i cant use it as a variable with inside the function so change it to : for square_list in numbers:
square_list.append(number**2) """ am i missing colons any were !!!"""


#7

or is it this what you mean:

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

def square():
for square_list in [number]:
square_list.append(5**2)
square_list.append(3**2)
square_list.append(1**2)
square_list.append(2**2)
square_list.append(4**2)
square_list.sort()

print square_list

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

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

print square_list


#8

im ok now i just copied someone else's code, sound for your time.

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

For

for number in start_list:
square_list.append(number**2) # Append
print square_list

Sort

square_list.sort()
print square_list


#9

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