亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 for 循環打印先前函數完成的操作?

如何使用 for 循環打印先前函數完成的操作?

夢里花落0921 2023-08-22 18:03:29
作為一項作業,我正在 python 中制作一個簡單的界面,它可以顯示余額、添加該余額、從該余額中取出資金、檢查利率,最后檢查之前完成的三個操作。這將是 choice == 5。那么我應該在底部 def 中寫什么才能做到這一點呢?usd = 500def main():    print("""    1. Balance    2. Add    3. Retrieve    4. Interest    5. Last Changes    """)    choice = int(input("Choose: "))    if choice == 1:        global usd        balance()    elif choice == 2:        add()    elif choice == 3:        retrieve()    elif choice == 4:        interest()    elif choice == 5:        changes()def balance():    global usd    print("Balance: ", round((usd),2))    main()def add():    global usd    amount = int(input("How much do you want to add? "))    usd = amount + usd    print("New balance = ", round((usd),2))    main()def retrieve():    global usd    amount = int(input("How much do you want to retrieve: "))    usd = usd - amount    print("New balance = ", round((usd),2))      main()def interest():    global usd    if usd<=1000000:        usd = ((usd/100)*101)        print("New balance: ", round(((usd/100)*101), 2))    elif usd>=1000000:        usd = ((usd/100)*102)        print("New balance: ", round(((usd/100)*102), 2))    main()def changes():         main()main()所需的輸出看起來有點像這樣;Choose: 5+6105-500000+1110000
查看完整描述

1 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

聽起來您想保留之前操作的日志。您可以通過創建一個列表并在每次完成操作時附加一個新條目來實現此目的。然后您的更改函數可以打印出列表中的最后 3 項。


您可以將該列表設置為全局并以與訪問相同的方式訪問它usd。


您還可以在 main 中創建列表并將其作為參數傳遞給更改。如果您決定這樣做,您可以讓每個函數返回其日志,以便您可以將其附加到 main 中的列表中。


例如(僅使用add函數說明)


使用全局變量(這是不好的做法,但更短):


usd = 500

log = []


def add():

? ? global usd

? ? amount = int(input("How much do you want to add? "))

? ? usd = amount + usd

? ? print("New balance = ", round((usd),2))

? ? log.append(f"+{amount}")? ? # add change to log

? ? main()


def changes():?

? ? # print last 3 items in log

? ? for change in log[-3:]:

? ? ? ? print(change)

? ? main()

或者更典型的方式,使用循環(沒有全局變量)


usd = 500


def main():

? ? log = []

? ? choice = 0

? ? while choice != 6:


? ? ? ? print("""

? ? ? ? 1. Balance

? ? ? ? 2. Add

? ? ? ? 3. Retrieve

? ? ? ? 4. Interest

? ? ? ? 5. Last Changes

? ? ? ? 6. Quit

? ? ? ? """)


? ? ? ? choice = int(input("Choose: "))


? ? ? ? if choice == 1:

? ? ? ? ? ? balance()

? ? ? ? elif choice == 2:

? ? ? ? ? ? log.append(add())? ? ? # add change to log

? ? ? ? elif choice == 3:

? ? ? ? ? ? log.append(retrieve()) # add change to log

? ? ? ? elif choice == 4:

? ? ? ? ? ? interest()

? ? ? ?elif choice == 5:

? ? ? ? ? ? changes(log)



def add():

? ? global usd

? ? amount = int(input("How much do you want to add? "))

? ? usd = amount + usd

? ? print("New balance = ", round((usd),2))

? ? return f"+{amount}"



def changes(log):?

? ? # print last 3 items in log

? ? for change in log[-3:]:

? ? ? ? print(change)


main()

這種方法的一個潛在問題是,因為您無限期地將項目添加到日志列表中,理論上您最終可能會耗盡計算機上的內存。為了解決這個問題,只要列表的長度大于 3,您就可以從列表中刪除多余的日志。


一般來說,全局變量是不好的做法,因此最好避免將 usd 設為全局變量,但我將其留給您


查看完整回答
反對 回復 2023-08-22
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號