Contains the launcher start.py and atm.py module user data is stored in the USERDATA.PKL user action log file by dictionary serialization Userid.record (save a record file for each user) commodity price file goods_list features include: withdrawal Deposit Transfer Shopping Print List Other instructions: No management program is included (for adding user accounts, product shelves), password authentication users can develop their own. start.py as follows:#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport getpassimport picklefrom atm import * #获取用户数据f1 =file (' userdata.pkl ', ' RB ') userinfo= Pickle.load (F1) f1.close () #主程序while true: userid = str (Raw_input ("\033[1; 32;40myour id:\033[0m ")) if userinfo.has_key (userid): while True: password = int (Getpass.getpass (' \033[1;32; 40menter password:\033[0m ')) if password != userinfo[userid][' passwd ']: print "\033[1;32;40mthe password is incorrect\033[0m" Continue else: print "\033[1;31;40mwenlcome %s.\033[0m" % userinfo[userid][' name '] show_menu () while True: input=int (raw_input (' Please select: ') if input == 1: money = Int (userinfo[userid][' youhave ')) show_balance (Money) elif input == 2: withdraw (userid,**userinfo) elif input == 3: Save_money (Userid,**userinfo) elif input == 4: transfer (Userid,**userinfo) elif input == 5: shopping (Userid,**userinfo) elif input == 6: history_list (userid) elif input == 7: sys.exit (0) else : print ' input error ' else: print "\033[1;31;40mthe userid is not vaild , please re-input:\033[0m " continueatm.py as follows: #!/usr/bin/python# -*- coding: utf-8 -*- import sysimport pickleimport reimport timeimport datetime# Print Menu Def show_menu (): print ' 1:balance inquiry ' print ' 2:withdraw money ' print ' 3:save money ' print ' 4:transfer ' print ' 5:shopping ' print ' 6:history list ' print ' 7: Exit ' #显示余额def show_balance (Money): print ' account balance:%d ' % money# withdrawal def withdraw (uid,**uinfo): draw=int (raw_ Input (' Please enter cash amount: ') &Nbsp; draw_str= '-%d ' % draw if uinfo[uid][' youhave '] < draw: print ' Insufficient balance ' else: uinfo[uid][' Youhave '] -= draw rest=uinfo[ uid][' youhave '] with file (' userdata.pkl ', ' WB ') as f1: pickle.dump (UINFO,F1) write_oper ( UID, ' withdraw ', draw_str,rest) #存钱def save_money (uid,**uinfo): save = int ( Raw_input (' Please enter deposit Amount: ') save_str= ' +%d ' % save uinfo[uid [' Youhave '] += save rest=uinfo[uid][' youhave '] write_ Oper (uid, ' Savemoney ', save_str,rest) #转账def transfer (uid,**uinfo): tran_id = raw_input (' Please enter the in-line account: ') if uinfo.has_key (tran_id): tran_money = int (raw_input (' Please enter transfer amount: ') uinfo[uid][' youhave '] -= tran_ money uinfo[tran_id][' Youhave '] += tran_money tran_money_str= '-%d ' % tran_money rest=uinfo[ uid][' youhave '] write_oper (UID, ' transfer ', tran_money_str,rest) #记录操作def write_oper (UID,O1,M1,R1): t1=time.strftime ('%y%m%d %h:%m:%s ', Time.localtime ( Time.time ()) with file ('%s.record ' % uid, ' a ') as f1: f1.write ('%s %s %s therest:%d\n ' % (T1,O1,M1,R1)) #购物 def shopping (uid,**uinfo): "atm.py" 118L, 3853c 48,7 Top # Shopping def shopping (uid,**uinfo): goods_dict={} goods&nbSp;= file (' goods_list ', ' r+ ') print ' product name price ' #打印可购买商品清单 for line in goods.readlines (): print line, key = Line.split () [0] value = int (Line.split () [1]) goods_dict[key] = value #购物车购物 ok= ' yes ' sh_total=0 while ok== ' yes ' : sh_cart=[] sh_ Choose=raw_input (' Select Product: ') if goods_dict.has_key (sh_choose) : sh_cart.append (Sh_choose) sh_total+=goods_dict[sh_choose] ok = raw_input (' Continue yes/no?: ') else :p rint "No this item, please re-enter" print "you select Item:%s total:%d ' % (sh_cart,sh_total) if uinfo[uid][' youhave '] < sh_total: print ' Insufficient balance ' else: uinfo[uid][' youhave '] -= sh_total with file (' Userdata.pkl ', ' WB ') as f1: pickle.dump (UINFO,F1) sh_total_str= '-%d ' % sh_total rest=uinfo[uid][' youhave '] write_oper (uid, ' shopping ', SH_TOTAL_STR, Rest) goods.close () #Query History list Def seach_list (Uid,start_date,end_date): f1=file ('%s.record ' % uid ) for line in f1.readlines (): cu_date=int (Line.split () [0]) if cu_date<=end_date and cu_date>=start_date: Print line, #查询历史清单主函数def history_list (UID): print ' 1: Query last one months ' print ' 2: Query last 3 months ' print ' 3: Select Time ' Input=int (raw_input (' Please select query time: ')] sys_time=int (time.strftime ('%y%m%d ', Time.localtime ( Time.time ())) now_time=datetime.datetime.now () thr_mon_ago_time =now_time-datetime.timedelta (days=90) one_mon_ago_time=now_time-datetime.timedelta (days =30) one_mon_ago=int (One_mon_ago_time.strftime ('%y%m%d ')) thr_mon_ago=int (Thr_mon_ago_ Time.strftime ('%y%m%d ')) if input == 1: seach_list (uid,one_mon_ago,sys_time) elif input == 2: seach_list (uid,thr_mon_ago,sys_time) 55,1 87% elif input == 2: seach_ List (uid,thr_mon_ago,sys_time) elif input == 3: start_d=int (raw_input (' Input start date (format: 20150301): ')) end_d=int (raw_input (' Input End date (format: 20150301): ')) seach_ List (uid,start_d,end_d) else: print ' Input error ' user data stored in userdata.pkluserinfo={' 10001 ' via dictionary serialization: {' passwd ': 123456, ' name ': ' Li ', ' youhave ':10000}, ' 10002 ': {' passwd ': 234567, ' name ': ' Skipper ', ' Youhave ':20000}} User Action log file 10001.record20150401 18:03:46 withdraw -221 therest:603420150401 18:07:39 savemoney +1001 therest : 703520150401 18:18:09 transfer -123 therest:591120150401 18:18:27 shopping -199 therest:5712 Commodity price Document goods_listcar 250000clothes 399shoes 199iphone5s 4999coffee 35foods 68bicycle 1688 Reference information: http://www.linuxidc.com/Linux/2014-09/107274.htm
This article is from "I bo so in" blog, please be sure to keep this source http://encoding.blog.51cto.com/1403836/1627430
Python Small Program ATM simulation