python之ATM(第五天)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之ATM(第五天)相关的知识,希望对你有一定的参考价值。
ATM程序简介:
1、登录login.py
2、取现cash.py
3、还款cashin.py
4、日志查询query.py
ATM主程序菜单
#!/usr/bin/env python
import sys,pickle
import pickle
import account,login,cash,cashin,query
def main(name):
while True:
print ‘‘‘\033[1m
Main Menu:
1.cash
2.cashin
3.query
4.quit
\033[0m‘‘‘
try:
one=int(raw_input(‘Choice one,input the num 1/2/3/4/5:‘).strip())
if one == 1:
cash.cash(name)
elif one == 2:
cashin.cashin(name)
elif one == 3:
query.query(name)
elif one == 4:
sys.exit()
else:
print "only accept 1/2/3/4"
continue
except ValueError:
print "only accept 1/2/3/4"
continue
name=login.login()
main(name)
login.py
import sys
import pickle
import time
def login():
account_file=file(‘account.pkl‘,‘rb‘)
account_info=pickle.load(account_file)
account_file.close()
a=0
global name
while True:
name=raw_input("Input your name:").strip()
if account_info.has_key(name):
if account_info[name][3] == "0":
a=0
while a<3:
pwd=raw_input("Input your password:").strip()
if pwd == account_info[name][0]:
now=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
f=file(‘login.log‘,‘a‘)
f.write("[%s] %s login in\n"%(now,name))
f.flush()
f.close()
return name
else:
print "error password"
a+=1
continue
else:
account_info[name][3]=1
account_file=file(‘account.pkl‘,‘wb‘)
pickle.dump(account_info,account_file)
account_file.close()
now=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
f=file(‘login.log‘,‘a‘)
f.write("[%s] %s locked\n"%(now,name))
f.flush()
f.close()
print "has input wrong pwd 3 times!!"
sys.exit()
else:
print "The user %s has been lock!"%name
sys.exit()
else:
print "error name"
continue
account.pkl 账户信息 用户名:admin 密码123 用户名admin2 密码123
(dp0
S‘admin‘
p1
(lp2
S‘123‘
p3
aS‘15000‘
p4
aF8070.0
aS‘0‘
p5
asS‘admin2‘
p6
(lp7
g3
ag4
aF14990.0
ag5
as.
account.py
import pickle
import os
if os.path.exists(‘account.pkl‘):
pass
else:
account_info={
‘administrator‘:[‘123‘,‘15000‘,‘15000‘,‘0‘]
}
account_file=file(‘account.pkl‘,‘wb‘)
pickle.dump(account_info,account_file)
account_file.close()
取现cash
import pickle
import time
def cash(name):
account_file=file(‘account.pkl‘,‘rb‘)
account_info=pickle.load(account_file)
account_file.close()
try:
get_num=int(raw_input(‘input the number you want to get(100*n):‘))
if get_num%100 == 0:
if get_num > account_info[name][2]:
print "sorry,not enough money,you just less $%s"%account_info[name][2]
else:
poundage=get_num*0.05
account_info[name][2]=int(account_info[name][2])-get_num-poundage
account_file=file(‘account.pkl‘,‘wb‘)
pickle.dump(account_info,account_file)
account_file.close()
now=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
record="Cash%s"%get_num
f=file(‘user.log‘,‘a‘)
f.write("%s\t%s\t%s\t$%d\t$%.2f\n"%(now,name,record,get_num,poundage))
f.flush()
f.close()
print "\033[1;31;40mCash $%s success,less $%s\033[0m"%(str(get_num),str(account_info[name][2]))
else:
print "sorry,only the number 100*n!"
except ValueError:
print "sorry,only the number 100*n!"
还款 cashin
import pickle
import time
def cash(name):
account_file=file(‘account.pkl‘,‘rb‘)
account_info=pickle.load(account_file)
account_file.close()
try:
get_num=int(raw_input(‘input the number you want to get(100*n):‘))
if get_num%100 == 0:
if get_num > account_info[name][2]:
print "sorry,not enough money,you just less $%s"%account_info[name][2]
else:
poundage=get_num*0.05
account_info[name][2]=int(account_info[name][2])-get_num-poundage
account_file=file(‘account.pkl‘,‘wb‘)
pickle.dump(account_info,account_file)
account_file.close()
now=time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))
record="Cash%s"%get_num
f=file(‘user.log‘,‘a‘)
f.write("%s\t%s\t%s\t$%d\t$%.2f\n"%(now,name,record,get_num,poundage))
f.flush()
f.close()
print "\033[1;31;40mCash $%s success,less $%s\033[0m"%(str(get_num),str(account_info[name][2]))
else:
print "sorry,only the number 100*n!"
except ValueError:
print "sorry,only the number 100*n!"
查询 query.py
import pickle
import time
def query(name):
account_file=file(‘account.pkl‘,‘rb‘)
account_info=pickle.load(account_file)
account_file.close()
mm=raw_input(‘Input the Month you want to query(2016-01):‘).strip()
if len(mm) == 0:
mm=time.strftime(‘%Y-%m‘,time.localtime(time.time()))
if len(mm) == 7:
print "User:%s\tLimit:$%s\tLess:$%.2f"%(name,account_info[name][1],account_info[name][2])
print "Month %s Bill:"%mm
print "\033[1mTime\t\t\tName\tRecord\tMoney\tPoundage\033[0m"
f=file(‘user.log‘)
for i in f.readlines():
a=i.split()
if name == a[2] and mm in a[0]:
print "%s %s\t%s\t%s\t%s\t%s\n"%(a[0],a[1],a[2],a[3],a[4],a[5]),
f.close()
else:
print "Invalid format"
日志文件 user.log
Time Name Record Money poundage
2016-02-27 16:29:46 admin Cash100 $100 $5.00
2016-02-27 16:29:48 admin Cash100 $100 $5.00
2016-02-27 16:29:50 admin Cash200 $200 $10.00
2016-02-27 16:29:52 admin Cash300 $300 $15.00
2016-02-27 16:30:14 admin Cashin $100 $0
2016-02-27 16:30:18 admin Cashin $500 $0
以上是关于python之ATM(第五天)的主要内容,如果未能解决你的问题,请参考以下文章