python核心编程9-10
2015-10-22 08:42
661 查看
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()
相关文章推荐
- Python文件操作读写文件
- 使用python对url编码解码
- python例子-Django项目中的模版特性
- 日常整理python执行系统命令的常见方法(全)
- python学习——基础(六)
- python 日期加减与转换为str
- eclipse python插件安装
- Windows10下安装配置python和django
- 【Python多进程库】一个函数让你设置CPU数和线程数
- Python -- 偏函数
- Python -- 生成器
- python 发送邮件,未完
- Python爬虫----抓取豆瓣电影Top250
- 09python生成文件脚本
- Window 下搭建OSQA系统
- Python -- 装饰器
- windows 装 python ipython
- 3.4 textwrap--格式化文本库(2)
- 64位的win7配置python、pip、numpy、scipy、matplotlib
- Python -- lambda, map, filter