Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条件if-else语句_Python_Jupyter Notebook - Fatal编程技术网

Python 条件if-else语句

Python 条件if-else语句,python,jupyter-notebook,Python,Jupyter Notebook,我想通过循环,每次价格超过初始价格时,我都会给财富增加1000英镑。每次循环小于初始价格时,我想从财富中减去1000。然后我想在所有迭代之后找到最终值。有人能帮忙吗?像这样: 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 >

我想通过循环,每次价格超过初始价格时,我都会给财富增加1000英镑。每次循环小于初始价格时,我想从财富中减去1000。然后我想在所有迭代之后找到最终值。有人能帮忙吗?

像这样:

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`
像这样:

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`

以下是正确的版本:

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

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

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)

以下是正确的版本:

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

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

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)