设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 7242|回复: 0
打印 上一主题 下一主题

关于【每日一坑】记账本

[复制链接]

0

主题

0

好友

52

积分

注册会员

Rank: 2

楼主
发表于 2020-10-29 23:24:25 |显示全部楼层
crossin先生 发表于 2016-8-19 11:06
可以的

crossin先生,请问这个记账本哪里需要改进吗?
  1. import os
  2. import datetime
  3. import re

  4. class Account():
  5.     def __init__(self):
  6.         # 数据存储的文件路径
  7.         self.file_name = "balance.txt"
  8.         self.file_path = os.path.join(os.getcwd(), self.file_name)

  9.         # 判断是否是首次记账,若否,则读出文件中的余额balance;若是,余额为0
  10.         try:
  11.             with open(self.file_path,"r", encoding="utf-8") as obj:
  12.                 balance_string = obj.readline().strip("\n")
  13.             self.balance = eval(balance_string)["balance"]
  14.             self.is_first = 0
  15.         except FileNotFoundError as e:
  16.             self.balance = 0
  17.             self.is_first = 1

  18.     def keep_accounts(self,item,money):
  19.         '''记账'''
  20.         with open(self.file_path, "a", encoding = "utf-8") as obj:
  21.             time = str(datetime.date.today())
  22.             line = time + " " + money + " " + item + "\n"
  23.             obj.write(line)

  24.         self.balance += float(money)


  25.     def check_the_balance(self):
  26.         '''查询余额'''
  27.         print(self.balance)


  28.     def check_the_details(self):
  29.         '''查询账单详情'''
  30.         try:
  31.             with open(self.file_path, "r", encoding = "utf-8") as obj:
  32.                 details_list = obj.readlines()
  33.                 for one in details_list:
  34.                     if one.startswith("{"):
  35.                         continue
  36.                     print(one)
  37.         except FileNotFoundError as e:
  38.             print("尚未有收支记录!")


  39. if __name__ == "__main__":
  40.     account = Account()
  41.     while True:
  42.         choice = input("选择操作:\n\t1、记账\n\t2、查余额\n\t3、收支明细\n").strip()
  43.         if choice == "1":
  44.             money = input("金额: ").strip()
  45.             item = input("名目: ").strip()
  46.             account.keep_accounts(item,money)
  47.         elif choice == "2":
  48.             account.check_the_balance()
  49.         elif choice == "3":
  50.             account.check_the_details()
  51.         elif choice == "leave":
  52.             break


  53.     with open(account.file_path,"r+", encoding ="utf-8") as obj:
  54.         data = obj.read()

  55.         # 若非首次记账,将新的余额替换旧的余额
  56.         if account.is_first == 0:
  57.             new_balance_string = '{{"balance":{}}}'.format(str(account.balance))
  58.             data = re.sub(r'{"balance":.*?}',new_balance_string,data)
  59.         # 若是首次记账,在文件头部记录每次记账结束后的余额
  60.         else:
  61.             new_balance_string = '{{"balance":{}}}\n'.format(str(account.balance))
  62.             data = new_balance_string + data

  63.         obj.seek(0)
  64.         obj.write(data)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-3 03:37 , Processed in 0.015374 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部