More for "for", need help


#1

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

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

i am getting this error ""square_list should be the same length as start_list""
please help.


More for "for"
#2

you have the right idea you just need to put the n**2 inside your square_list.append
so it should look like this

square_list.append(n**2)

this puts the squared numbers inside square_list
also indent your square_list.sort and append so it is inside the loop


#3

correct, i think that you are excellent for a 'student coder'


#4

haha thanks just started taking this in computer science so I'm still a student there just relaying info I know onto others :smile:


#5

Ive just seen the answer above, thanks.

My own code, which I arrived at independently is below. Can anyone tell me where I am going wrong please? I sat with a pencil and piece of paper and logically worked out the steps that I need to make the code work.

The problem for me seems to be the grammar rules of Python I can never for the life of me remember where the '.sort' goes or whether there is a 'sort()' bracket after something. Sorry if I am no explaining myself well. Its all about the grammar for me. Its frustrating because if one character or space is out of place the whole thing just doesn't work.

here is my code (sorry I don't know how to embed it properly)

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

for number in start_list
number == number ** 2

number.append(square_list)

.sort(square_list)

print square_list


#6

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

Your code here!

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

print square_list


#7

right code

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

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


#8

Thanks for this uploaded question!
Not to change the subject, now I got the correct code, but having a question about 'return'.
Before I saw this, I wrote the code presented below and it is said to be invalid syntax.

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

Why can't 'return' be used in this code?
Though it may be a very easy thing, I'm confused with using 'return' or not as a beginner.


#9

@microace55363: If that code is not inside a function, it won't work. return statements can only be used in functions, so you can get a specific value from it and bring back to calculation, comparison or something else outside of the function.

Also, the append() function doesn't returning a value so doing that is sort of pointless.

Q: How does the return work?
A: It's like this, the execution of your program will go executing your lines of code. Whenever the execution encounters a function call, it will stop the execution, go to where the function has been declared, execute the commands that are part of your function and if a given condition is executed, like if x % 2 == 0: return True, it will simply ignore the rest of the code, inside of that function, that goes after the return statement and go back to where the function was called.

If I wasn't clear, let me know and I'll find another way to explain!