• 80-简单的消费记录程序


    记录账单功能的简单程序,本金10000,带有记录消费或者新增的功能:

    import pickle
    import os
    import time
    
    def cost(wallet, record):  #
        amount = int(input('amount: '))
        comment = input('comment: ')
        date = time.strftime('%Y-%m-%d')
        with open(wallet, 'rb') as fobj:
            balance = pickle.load(fobj) - amount
        with open(wallet,'wb') as fobj:
            pickle.dump(balance,fobj)
        with open(record,'a') as fobj:
            fobj.write(
                    '%-12s%-8s%-8s%-10s%-20s
    ' % (date, amount, '', balance, comment)
            )
    
    
    def save(wallet, record):  # 记录存钱的函数
        amount = int(input('amount: '))
        comment = input('comment: ')
        date = time.strftime('%Y-%m-%d')
        with open(wallet, 'rb') as fobj:
            balance = pickle.load(fobj) + amount
        with open(wallet, 'wb') as fobj:
            pickle.dump(balance, fobj)
        with open(record, 'a') as fobj:
            fobj.write(
                '%-12s%-8s%-8s%-10s%-20s
    ' % (date, '', amount, balance, comment)
            )
    
    
    def query(wallet, record):  # 查询收支明细的函数
        print('%-12s%-8s%-8s%-10s%-20s' % ('date', 'cost', 'save', 'balace', 'comment'))
        with open(record) as fobj:
            for line in fobj:
                print(line, end='')
        with open(wallet, 'rb') as fobj:
            balance = pickle.load(fobj)
        print("Latest Balance: %d" % balance)
    
    
    def show_menu():
        cmds = {'0': cost, '1': save, '2': query}
        prompt = """(0) cost
    (1) save
    (2) query
    (3) exit
    Please input your choice(0/1/2/3): """
        wallet = 'wallet.data'
        record = 'record.txt'
        if not os.path.exists(wallet):
            with open(wallet, 'wb') as fobj:
                pickle.dump(10000, fobj)
    
        while True:
            try:
                choice = input(prompt).strip()[0]
            except IndexError:
                continue
            except (KeyboardInterrupt, EOFError):
                print()
                choice = '3'
    
            if choice not in '0123':
                print('Invalid input. Try again.')
                continue
    
            if choice == '3':
                break
            cmds[choice](wallet, record)
    
    if __name__ == '__main__':
        show_menu()

    结果输出:

    (0) cost
    (1) save
    (2) query
    (3) exit
    Please input your choice(0/1/2/3): 0
    amount: 200
    comment: 买衣服
    (0) cost
    (1) save
    (2) query
    (3) exit
    Please input your choice(0/1/2/3): 1
    amount: 1000
    comment: 兼职
    (0) cost
    (1) save
    (2) query
    (3) exit
    Please input your choice(0/1/2/3): 2
    date        cost    save    balace    comment             
    
    2019-06-18  200             9800      买衣服                 
    2019-06-18          1000    10800     兼职                  
    Latest Balance: 10800
    (0) cost
    (1) save
    (2) query
    (3) exit
    Please input your choice(0/1/2/3): 
  • 相关阅读:
    SpringBoot入门学习(二)
    SpringBoot入门学习(一)
    eclipse调试程序界面简单介绍使用
    利用URLConnection来发送POST和GET请求
    查看用户所属的组
    linux下创建用户,给用户设置密码,给用户授权
    新linux系统上rz 与sz命令不可用
    pom.xml文件报错:web.xml is missing and <failOnMissingWebXml> is set to true
    MySql采用GROUP_CONCAT合并多条数据显示的方法
    Mysql计算时间差
  • 原文地址:https://www.cnblogs.com/hejianping/p/11044903.html
Copyright © 2020-2023  润新知