首页 > 解决方案 > 条件 if else 语句

问题描述

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。然后我想在所有迭代之后找到最终值。有人能帮忙吗?

标签: pythonjupyter-notebook

解决方案


像这样:

In [191]: for price in prices: 
     ...:     if price > init_price: 
     ...:         wealth += 1000 
     ...:     else: 
     ...:         wealth -= 1000 
     ...:                                                                                                                                                                                                   

In [192]: wealth                                                                                                                                                                                            
Out[192]: -5000

推荐阅读