9. More with 'For'


#1



9. More with 'for'


Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: invalid syntax (python, line 4)


I dont know what i expected to happen...


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

for start_list.append()
sort square_list


print square_list


#2

Changed my code, still doesn't work here is my new code...:

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

for start_list.append()
square_list.sort()

print square_list


#3

And here's the error message...

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: invalid syntax (python, line 4)


#4

I'm assuming you have to fill the square_list with the original numbers squared.

First, check the syntax in your for statement! It's missing a few important things:

for something in somewhere:
    #do something that's indented

So when you wanna iterate through a list you do it like this:

for x in myList:
    #do magic with your x element

OK then, let's iterate through start_list and do something with every element in each cycle:

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

for x in start_list:
    #maybe append the x element (squared) to the other list in each cycle?

And then at the end of the for you could rearrange and print the new list:

square_list.sort()
print (square_list)

Update your code and let me know if it worked!


#5

This is what the Box says:

File "python", line 4
for start_list.append()
^
SyntaxError: invalid syntax


#6

Imma edit that, that ain't right...
File "python", line 4
for start_list.append()
^
SyntaxError: invalid syntax


#7

Argh its still in the wrong place
^^^


#8

Hmm... you ned to read slowly what I've wrote earlier lol.

You're not using your for the way it's got to be used.

I don't know if giving you the straight answer will help you since you appear to be very lost my friend.

Don't worry yet about the appending, sorting and printing. Think for now HOW you got to go trough your start_list, using the for cycle. How'd you do it?

Hint: I've alerady said it!


#9

Thankyou cause your comments helped me. Appreciate it. I was going to post the answer but I dont want to step on toes lol ill let you continue to help this person


#11

can you post an updated version of your code?


#12

Sorry i didnt look here for a while and sure :slight_smile:


#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

#14

I'm now finished xD :sunny:


#15

Oh and thanks for the help here is my updated code

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

#16

Weird... i'm getting notifications, but i can't see any reply's or anything... -_-


#17

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