這也許很簡單,但我怎樣才能使其自動化。 例如,這只是2個步驟,但我想對100個步驟做同樣的事情。 我應該使用回圈或其他型別的函式嗎?
def earn(w,y)。
return w y
def spend(w,x)。
new_wealth = w -x
if new_wealth < 0:
print("Insufficient funds"/span>)
else:
return new_wealth
w0=0: return new_wealth
w1=earn(w0,2300)
w2=支出(w1,1500)
w3=收入(w2,2300)
w4=支出(w3,1500)
print("w0,w1,w2,w3,w4 = ", w0,w1,w2,w3,w4)
uj5u.com熱心網友回復:
如果你有一個交易的串列,其中有花錢還是掙錢以及價值,你可以嘗試這樣的東西
transactions = ['ear 2300'/span>, 'spend 1500', 'ear 2300', 'spend 1500']
字串 = ''
結果 = [0]
for n in range(len(actions)):
string = 'w' str(n)
transactions_type = transactions[n].split(' '/span>)[0]
transactions_value = int(transactions[n].split(' '/span>)[1]
if transactions_type.lower() == 'earn':
results.append(earn(results[-1], transactions_value)
if transactions_type.lower() == 'spend'。
results.append(spend(results[-1], transactions_value)
output = string ' = 'for value in results:
output = str(value)
其中的輸出
w0,w1,w2,w3, w4 = 0 2300 800 3100 1600
但這將取決于你如何存盤這個交易資訊,這里的方法是可行的,但并不理想
。uj5u.com熱心網友回復:
為了重復同一個動作,你顯然應該使用回圈。 下面是一個使用 "for "回圈的例子,其中有100次的迭代,將每個操作結果附加到一個串列中:
results = []
你顯然應該根據你的目的修改它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309266.html
標籤:
