python做一个记账本程序代码
时间: 2023-10-06 12:05:27 浏览: 56
好的,我可以为你提供一个基本的记账本程序代码,如下所示:
```
class AccountBook:
def __init__(self):
self.records = []
def add_record(self, record):
self.records.append(record)
def show_records(self):
for r in self.records:
print(r)
class ExpenseRecord:
def __init__(self, date, category, amount):
self.date = date
self.category = category
self.amount = amount
def __str__(self):
return f"Expense {self.date}: {self.category} - {self.amount}"
account_book = AccountBook()
while True:
print("请选择操作:")
print("1. 添加一条记录")
print("2. 查看记录")
print("3. 退出程序")
choice = input("请输入数字:")
if choice == "1":
date = input("请输入日期(例如:2022-01-01):")
category = input("请输入类别:")
amount = input("请输入金额:")
record = ExpenseRecord(date, category, amount)
account_book.add_record(record)
print("记录添加成功!")
elif choice == "2":
account_book.show_records()
elif choice == "3":
break
else:
print("输入有误,请重新输入!")
```
这段代码实现了一个简单的记账本程序,可以通过输入日期、类别和金额添加支出记录,并可以查看已有的所有记录。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)