More with 'for'


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

Why do I need to have the length the same and how do I do that?

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

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

print square_list


If you square each value, then you should be ending up with as many squared values as there are original values, because for each original value, there will be one squared value that you should add

You may want to just read your code because it doesn't quite add up. Make sure you have a firm idea of what's supposed to be done so that you have something to compare your code to.


I am just not getting this and I've really tried everything I can think of. I seem to be having trouble with .append() more than anything else. No matter what value I insert in () it always come back invalid.


Why is list's append the cause of trouble? It'll accept any value because lists can refer to any values. If something "comes back invalid" then you'll need to start comparing what it did to what it should have done - there should not be any guessing involved, look for the difference and modify the code to eliminate that difference. And if you don't know what to modify, then keep studying what it's doing step by step. If you're having trouble with getting some particular action to work, then write another piece of code that only carries out that action so that you can figure it out in isolation from the rest of your program.


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