python核心编程9-10

9–10. 家庭理财. 创建一个家庭理财程序. 你的程序需要处理储蓄, 支票, 金融市场, 定期存款等多种帐户. 为每种帐户提供一个菜单操作界面, 要有存款, 取款, 借, 贷等操作. 另外还要提供一个取消操作选项. 用户退出这个程序时相关数据应该保存到文件里去(出于备份的目的,程序执行过程中也要备份.)

有两天没学写程序了,好堕落。


def deposit(money,string):
    '''
存/取款
'''
    prompt = """
(Y)确认
(N)返回
Enter choice: """
    if string==1:
        plus=int(input('请输入您存入的金额:'))
    if string==2:
        plus=-int(input('请输入您取出的金额:'))
    done = False
    while not done:
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'n'
            print ('\nYou picked: [%s]' % choice)
            if choice not in 'yn':
                print ('invalid option, try again')
            else:
                if choice == 'y':
                    money=money+plus
                    return money
                    break
                done = True


def loan(loans,choise):
    '''
取款
'''
    prompt = """
(Y)确认
(N)返回
Enter choice: """
    if choise==1:
        plus=int(input('请输入你要借出的金额:'))
    if choise==2:
        plus=-int(input('请输入您贷入的金额:'))
    done = False
    while not done:
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'n'
            print ('\nYou picked: [%s]' % choice)
            if choice not in 'yn':
                print ('invalid option, try again')
            else:
                if choice == 'y':
                    loans=loans+plus
                    return loans
                    break
                done = True


def showmenu():
    '''
主菜单
'''
    prompt = """
(1)存款
(2)取款
(3)借款
(4)贷款
(5)显示
(Q)uit
Enter choice: """

    done = False
    f=open('c:/python34/learn/data','r')
    if 'money' not in f.read():
        g=open('c:/python34/learn/data','w')
        g.write('money:0\nloan:0')
        g.close()
    f.seek(0)
    a=f.readline().strip()
    b=f.readline().strip()
    money=int(a[6:])
    loans=int(b[5:])
    while not done:
        
        chosen = False
        while not chosen:
            try:
                choice = input(prompt).strip()[0].lower()
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print ('\nYou picked: [%s]' % choice)
            f=open('c:/python34/learn/data','r')

            f.close()
            if choice not in '12345q':
                print ('invalid option, try again')
            else:
                
                if choice == '1':
                    money=deposit(money,1)
                     
                elif choice == '2':
                    money=deposit(money,2)
      
                elif choice == '3':
                    loans=loan(loans,1)
     
                elif choice == '4':
                    loans=loan(loans,2)
                elif choice == '5':
                    print('money:%d'%money)
                    print('loan:%d'%loans)
                  
                else:
                    print ('quit!')
                    w=open('c:/python34/learn/data','w')
                    line='money:%d'%money+'\n'+'loan:%d'%loans
                    w.write(line)
                    w.close()
                    return

if __name__ == '__main__':
    showmenu()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值