我正在嘗試構建一個預算應用程序,它將幫助我更好地構建和管理資金。我已經確定我需要管理三種類型的費用: - 每日(如交通費用、工作零食) - 每周購買(如雜志、書籍) - 每月費用(租金、水電費)我已經用 Python 編寫了一個通用類,它可以捕獲所有這些,但我想給自己更詳細的信息,說明每個費用類每月花費我多少,并可能把它放到日歷上。我想知道如何將上述費用類型實現為類,并將它們全部寫入一個公共類,在那里它們可以分類和格式化為易于識別的組,如“每日”、“每周”和“每月”但下降在一個通用界面下,我可以在上述結構中匯總費用,以便快速查看和計劃等。有什么建議么?我對 Python Coding 非常陌生——到目前為止,這個想法讓我很難過代碼:class Expenses:def __init__(self): self.list = []"""Create dict containing expense"""def add_expense(self, name, cost, describe): val = {"Name": name, "Cost": cost, "Description": describe } return self.list.append(val)"""Show all of the expenses contained in the list self.list"""def list_expenses(self): for i in self.list: for k, v in i.items(): print('%s: %s' % (k, v))"""add up the total of all expenses in self.list"""def total_expenses(self): total = 0 for i in self.list: for k, v in i.items(): if k == "Cost": total += v return total"""remove the given expense I should create a key that can be used as an indexer"""def delete_expense(self, num): return self.list.pop(num)這可以按我的意愿工作 - 但就像我也試圖暗示一樣,我想為這門課添加更多細節,我正在使用它來學習 OOP。我不介意使用普通的命令式代碼
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
我真的相信你想多了,但無論如何我認為你的問題最簡單的解決方案是在你的根類中添加另一個屬性。
此屬性將確定time_span
費用(每天、每周或每月),
然后您將能夠實現諸如daily_expenses(date)
,weekly_expenses(time_range)
或monthly_expenses(datetime_month)
(您的時間范圍或日期選項實際上是無窮無盡的......)
添加回答
舉報
0/150
提交
取消