标签云

微信群

扫码加入我们

WeChat QR Code

I have two lists and I wanted to create generate the quotients using a for loop. Specifically, I would want to have .5 and .25 and .125 and in that order. Of course, I could just take these values and determine the quotients manually, but I am looking at this from the perspective of someone who is taking their first stab at learning Python. I have read some other articles on for loops, but do not see why the below produces :index error: list index out of range. Thus, I am looking not only for the modification of this code but also a reasonable explanation as to why there is such an error.x=[2,4,16]y=[4,16,128]for i in y:y[i]/x


for i in y what do you think i contains? (ps. its not index numbers)

2019年12月07日31分29秒

By doing for i in y: you are directly iterating over the elements of y. You need enumerate for your purpose. Or even better zip

2019年12月07日31分29秒

This question has been previously marked as off-topic. I believe it's the same you in another name.

2019年12月07日31分29秒

What is wrong directly iterating over the elements of y? I do not get that

2019年12月07日31分29秒

Jack nothing, until you type this. y[i]. Think what value i has. What does that resolve to?

2019年12月07日31分29秒

what is the meaning of f in print(f"i: {i}")

2019年12月07日31分29秒

also this is not dividing anything. this just re-lists everything in a different way

2019年12月07日31分29秒

print(f"i: {i}") is a f-string. It's a way to format print statements. And it's not dividing anything because I'm showing you how to correctly index through a list.

2019年12月07日31分29秒

You should use value/x[index]

2019年12月07日31分29秒