prices = [80, 102, 84, 41, 74, 55, 32, 77, 92, 40, 91, 26]
wealth = 5000
init_price = 100
buy = []
sell = []
x = 0
for i, v in enumerate(prices):
if (v > init_price):
buy = wealth + 1000
continue
else:
buy_1 = buy.append(buy - 1000)
continue`我想通过循环运行,每次价格超过初始价格时,我会增加1000的财富。对于每一次循环小于初始价格,我想从财富中减去1000。然后我想在所有迭代之后找到最终的值。有人能帮上忙吗?
发布于 2020-05-08 21:08:48
如下所示:
In [191]: for price in prices:
...: if price > init_price:
...: wealth += 1000
...: else:
...: wealth -= 1000
...:
In [192]: wealth
Out[192]: -5000发布于 2020-05-08 21:10:42
下面是正确的版本:
prices = [80, 102, 84, 41, 74, 55, 32, 77, 92, 40, 91, 26]
wealth = 5000
init_price = 100
sell = []
x = 0
for v in prices:
if (v > init_price):
wealth += 1000
else:
wealth -= 1000
print(wealth)结果:
-5000https://stackoverflow.com/questions/61679869
复制相似问题