Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 Python 中编写以下累积值
如果我有
x=1, y=2 cum1=x*y+0=2 cum2=x*y+cum1=4 cum3=x*y +cum2=6
等等
您想多次重复相同的操作,因此您可以使用保持前一个值的循环。
x = 1 y = 2 previous = 0 for i in range(0, 10): new = x*y + previous print(new) previous = new # output: 2, 4, 6, 8, 10 etc..