9 more with 'for'


#1



I would like to find out what wrong with my code

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

I have try to print out "x", it work finely in 1st place, but when append into square_list the result of "x" have change back to the original data in start_list. and the system give me this error

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


is there any Loop expert can tell me what wrong with my code? I not asking the correct answer for just passing the quest


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

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


#2

Hi @systemcoder31536 ,

Here, you are printing x ** 2, but then appending x to square_list, without squaring it ...

    print x ** 2
    square_list.append(x)

Also consider when you should sort the new list. Currently, you are sorting square_list within the loop.


#3

Python is indentation sensitive. Don't indent square_list.sort() and your code should work! :slight_smile:


#4

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