day 06 简单的加减法数学游戏

简单的加减法数学游戏
1.  随机生成两个100以内的数字
2.  随机选择加法或是减法

3.  总是使用大的数字减去小的数字
4.  如果用户答错三次,程序给出正确答案

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\n' % (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\n' % (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()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值