完全没有异常处理。还在学习中。。。部分代码还能再写成函数。

#!/usr/bin/python
import sys
import pickle
import os
import getpass
import time
def login(username = '',password = '',user_type = 1):print 'Welcome 2 ATM Basic System.'error_count = 0initInfo(info_list)pkl_file = file('userInfo.pkl','rb')dic_account = pickle.load(pkl_file)pkl_file.close()while True:username = str(raw_input('please input your username or quit(q): ').strip())if len(username) == 0:print 'Invalid Username!'elif username in ('q','quit'):sys.exit()elif username not in dic_account.keys():print 'No %s found!' % usernameelse:password = str(dic_account[username][0])type = int(dic_account[username][-1])if type == 2:print 'Account %s is Lock!Please contact your Admin.' % usernamesys.exit()else:while True:pass_input = getpass.getpass('please input your password: ')if pass_input != password:print 'Wrong Password!'error_count += 1if error_count == 3:print 'You give the wrong password 3 times.now exit!'dic_account[username][-1] = 2dicDump(dic_account)sys.exit()else:continueelse:if type == 1:while True:print 'Welcome,%s' % usernameprint '*'*20print '1.Go Shopping'print '2.Withdraw'print '3.Repay'print '4.Show Detail'print '0.quit'print '*'*20choice = int(raw_input('Please choose one option: ').strip())if choice == 1:goShopping(username,dic_account)elif choice == 2:withDraw(username,dic_account)elif choice == 3:rePay(username,dic_account)elif choice == 4:showDetail(username,dic_account)elif choice == 0:sys.exit()else:print 'please choose one option with int 1/2/3/4/0.'else:while True:print 'Welcome!Admin %s' % usernameprint '*'*20print '1.Add User'print '2.Edit User'print '3.Show All'print '0.quit'print '*'*20choice = int(raw_input('Please choose one option: ').strip())if choice == 1:addUser(dic_account)elif choice == 2:editUser(dic_account)elif choice == 3:showAll(dic_account) elif choice == 0:sys.exit()else:print 'please choose one option with int 1/2/3/4/0.'
def initInfo(info_list):if not os.path.exists('userInfo.pkl'):pkl_file = open('userInfo.pkl','wb')pickle.dump(info_list,pkl_file)pkl_file.close()else:pass
def dicDump(dic_account):pkl_file = open('userInfo.pkl','wb')pickle.dump(dic_account,pkl_file)pkl_file.close()def goShopping(username,dic_account):shopList = {'Computer':4999,'Phone':3999,'Music Player':1688,'Coffee':27}for item in shopList:print '%s => %s' % (item,shopList[item])while True:each = raw_input('which one are you gonna buy?').strip()if each in ('q','quit','exit'):breakelif each not in shopList.keys():print 'No %s in the shopping list!retry!' % eachelse:if dic_account[username][2] >= shopList[each]:f = file('detail.log','a')f.write('%s\t%s\t%s\t%s\t0\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),each,shopList[each]))f.close()dic_account[username][2] -= shopList[each]dicDump(dic_account)print 'You have bought %s,it takes you %d yuan.now you have a balance of %d.' % (each,shopList[each],dic_account[username][-2])else:print 'You have not enough money to offord it!'break
def withDraw(username,dic_account):draw_count = int(raw_input('How much Cash withdrawal? '))while True:if (draw_count*105/100) > dic_account[username][2]:print 'Oops!You have not enough Amount to Cash withdrawal!Your balance now is %s !'% (dic_account[username][2])breakelse:dic_account[username][2] -= draw_countdicDump(dic_account)f = file('detail.log','a')f.write('%s\t%s\tCash withdrawal\t%s\t%s\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),draw_count,(draw_count*5/100)))f.close()print 'For now,your balance is %d'% (dic_account[username][2])break
def rePay(username,dic_account):save_count = int(raw_input('How much Cash to save? '))dic_account[username][2] += save_countdicDump(dic_account)f = file('detail.log','a')f.write('%s\t%s\tRepay\t%s\t0\n' % (username,time.strftime('%y-%m-%d %H:%M:%S',time.localtime()),save_count))f.close()print 'For now,your balance is %d'% (dic_account[username][2])
def showDetail(username,dic_account):if not os.path.exists('detail.log'):print 'Your Detail List is Empty!'print 'Your max permit is %s,now your balance is %s.\n'%(dic_account[username][1],dic_account[username][2])else:print 'Your max permit is %s,now your balance is %s.\n'%(dic_account[username][1],dic_account[username][2])f = file('detail.log','r')for eachLine in f:if eachLine.startswith(username):print eachLineelse:passf.close()
def addUser(dic_account):add_username = str(raw_input('What\'s the user\'s name? ').strip())if len(add_username) == 0:print 'Invalid User Name!'elif add_username.lower() in dic_account.keys():print 'User Already Exists@!'else:while True:add_passwd = getpass.getpass('Please input the password: ')if len(add_passwd) < 6:print 'Password\'s len must longer than 6 letters!'breakelse:add_passwd_again = getpass.getpass('Please input the password again: ')if add_passwd_again != add_passwd:print 'password is\'t same as the first time input!'breakelse:add_user_balance = int(raw_input('Define a max balance number: '))dic_account[add_username] = [add_passwd,add_user_balance,add_user_balance,1]dicDump(dic_account)print 'Add %s success!Amount is %d.'% (add_username,add_user_balance)break
def editUser(dic_account):edit_username = str(raw_input('Which user do you want to edit? ').strip())if len(edit_username) == 0:print 'Invalid User Name!'elif edit_username.lower() not in dic_account.keys():print 'User Doesn\'t Exists@!'else:while True:print '\n\t1.Change The Max Balance'print '\t2.Change Password'print '\t3.Lock/Unlock The User'edit_choice = int(raw_input('Please Choose one option or quit!: '))if edit_choice == 1:edit_user_balance = int(raw_input('Define a max balance number to Change: '))dic_account[edit_username][1] = edit_user_balancedicDump(dic_account)breakelif edit_choice == 2:edit_user_passwd = getpass.getpass('Please input the password : ')dic_account[edit_username][0] = edit_user_passwddicDump(dic_account)breakelif edit_choice == 3:edit_user_type = int(raw_input('Lock => 2,Unlock => 1: '))dic_account[edit_username][-1] = edit_user_typedicDump(dic_account)breakelse:print 'Please retry,just use the int 1/2/3!'break
def showAll(dic_account):for each_user in dic_account.keys():print '*'*20print '%s\'s Max Amount is %d,now Balance is %d.\n' % (each_user,dic_account[each_user][1],dic_account[each_user][2])f = file('detail.log','r')for eachLine in f:if eachLine.startswith(each_user):print eachLineelse:passf.close()
info_list = {'fengxsong':['p@55w0rd',15000,15000,0],'nagios':['nagios',9000,9000,1]}
if __name__ == '__main__':login()

转载于:https://blog.51cto.com/fengxs/1404354

python小作业初版之信用卡交易相关推荐

  1. 【python小作业】编写函数,接收一个任意字符串,返回其中最长的数字字串。要求使用正则表达式。

    题目 编写函数,接收一个任意字符串,返回其中最长的数字字串.要求使用正则表达式. python题解 使用正则表达式re.findall,可以找到所有的数字, 其中+表示前面的字符匹配1次或多次. \d ...

  2. python小作业8代码(列表的遍历与嵌套)

    任务一:斐波纳契数列 描述:从第三项开始,后一项是前两项的和 代码: list=[1,1] for i in range(18):list.append(int(list[i])+int(list[i ...

  3. 右耳Python小作业---学员分组

    stu_list = [['李渊', 82], ['李世民', 7], ['侯君集', 5], ['李靖', 58], ['魏征', 41], ['房玄龄', 64], ['杜如晦', 65], [' ...

  4. Python小作业 列举红黄绿小球的组合

    有红.黄.蓝三种颜色的球,其中红球 3 个,黄球 3 个,绿球 6 个.先将这 12 个球混合放在一个盒子中,从中任意摸出 8 个球,编程计算摸出球的各种颜色搭配. 代码如下: i = 1 for n ...

  5. python小作业6代码(字符串的实际应用)

    任务一:密码破解程序 任务内容: #加密是ASCII+5 #那么解密就是ASCII-5 str='ixo678' for i in str:a=ord(i)-5b=chr(a)print(b,end= ...

  6. Python011: Python大作业之移动的小火车动画(四)代码实现

    书接上文:Python010: Python大作业之移动的小火车动画(三)结果显示 0.注意: ​ 该项目使用的库和资源说明如下: pygame 2.0.1 (SDL 2.0.14, Python 3 ...

  7. pygame飞机大战小游戏(python大作业)

    一.项目背景 python大作业,在查看了老师给的链接发现教学视频不完整,所以借用了同学的<Python编程 从入门到实践>中的一个项目,学习模仿. 二.游戏具体介绍 这是一款由辉辉亲自打 ...

  8. Python008: Python大作业之移动的小火车动画(一)

    Python大作业 1. 要求 使用Python语言,使用的库不限 如下图1的火车轨道 如下图2的火车(2节车厢) 小火车可以在轨道上动态的运行 小火车的大小可调.轨道的大小可调 2. 用到的第三方库 ...

  9. 风变python小课 基础语法12 作业1_菜鸟的风变编程Python小课之路,这么学编程也可以?...

    原标题:菜鸟的风变编程Python小课之路,这么学编程也可以? 作为职场菜鸟,我感觉我就是现实生活里的孙弈秋,虽然学历没有他那么惨,但是在公司总感觉不那么受待见,可能因为我们这个行业本身竞争大吧,领导 ...

最新文章

  1. mysql8 mac 忘记密码_mac下 MySql 8.0.15忘记密码重置密码
  2. 【Python初级】009-错误与异常
  3. 解决Geoserver请求跨域的几种思路
  4. python比较两张图片是否一样_opencv_判断两张图片是否相同
  5. git commit -m 提交时报错husky pre-commit (node v12.18.2)
  6. python基础入门(5)之运算符
  7. PSPNet网络要点
  8. Spring boot 通过ApplicationRunner在启动完成后按指定顺序执行任务
  9. css简单的数学运算
  10. 关于Microsoft Edge主页被360劫持
  11. 排水管网计算机模拟,基于SWMM的城市合流制排水管网计算机模拟方法.ppt
  12. 微信分享wx.config配置时遇到invalid signature错误的解决办法
  13. 全面解析机房综合布线结构、设计方案及未来发展趋势
  14. html 超链接打开pdf,HTML利用超链接打开链接文件
  15. 虚无鸿蒙混沌系统,玄幻 鸿蒙混沌选择系统
  16. python 文件读取
  17. 如何设置计算机桌面待办事项,怎么在Windows电脑桌面便签上显示未来要处理的待办事项?...
  18. 重庆大学科幻协会发展史
  19. ABTest流量分发和业界的一些经验
  20. 博士申请 | 卡耐基梅隆大学陈贝迪老师课题组招收机器学习方向博士生

热门文章

  1. B-004 LC滤波器的基础知识
  2. 阳志平:思维的抽象层级-逻辑链条的起点
  3. 第一性原理·非线性成长·人生模式
  4. MySQL优化器成本记录表
  5. 使用Example_where_Cause出现 Column 'goods_id' in where clause is ambiguous解决办法
  6. 第五代TTS语音芯片SYN8086性能再突破
  7. 包装印刷行业裕同集团易普优APS项目顺利验收!
  8. 浮云E绘图之多点连线源码
  9. 不知道视频如何裁剪画面大小不变?来看看这篇文章
  10. 首都师范大学计算机考研调剂,2018年首都师范大学考研调剂信息