Code and still gives me weird error


#1

start_list = [5, 3, 1, 2, 4]
square_list = []
for numbers in start_list:
square_list.append(numbers ** 2)
square_list.sort()
print square_list

This is my code and still gives me weird error ;/


More with 'for'
#2

refresh page,reset page,do it again and post error...
u watched for indentation in square list append line?


#3

I did everything you sir said , and the error message is:
File "python", line 4
square_list.append(numbers ** 2)
^
IndentationError: expected an indented block


#4

u need to hit space keyboard 4 times in that line=indentation


#5

start_list = [5, 3, 1, 2, 4]
square_list = []
for numbers in start_list:
square_list.append(numbers ** 2)

square_list.sort()
print square_list

because of no block formation in for ...after for in the next line where u write the append statement give 4times space


#6

Yes, you need to indent the 2 middle lines like this:

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

#7

Identation?
That worked for me.


#8

When I ran the code I was thinking that numbers would come back as undefined but it worked perfectly. I'm confused as to how "numbers" is used here and where it comes from.


#9

The for..in statement defines a block variable which holds a temporary item as the list is iterated. numbers is that variable. It is actually singular though, so I would use number. The list is what is plural (of course).

Given a list,

eg = [2,3,5,7,11,13,17,19]
for item in eg:
     print item,
print

will output,

2 3 5 7 11 13 17 19

#10

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