主要还是参考网上内容,自己做了修改。虽然代码有小bug,但是不影响学习和测试。
功能:
1.额度:8000
2.可以提现,手续费5%
3.每月最后一天出账单,写入文件
4.记录每月日常消费流水
5.提供还款接口
1.atm的脚本[root@python atm]# cat atm.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Date:2017-03-23
Author:Bob
'''
import os
import time
import pickle
import readline #解决退格键和上下键引起的乱码,需要安装readline和readline-devel包
#定义账单,商品和购物车
Bill = {}
products = {}
shoplist = []
#define Bill function, used to record billing details(account/time/describe/money).
def Bill(Account,Time,Description,RMB):
Bill = {"Account":Account,"Time":Time,"Description":Description,"RMB":RMB}
#用pickle模块把账单信息存入到bill文件中去
pickle.dump(Bill,open("bill","a"))
#购物功能
def shop():
print '\033[;32mWelcome to shopping!\n\033[0m'
with open('shops.txt') as f:
for line in f.readlines():
print '{}'.format(line.strip())
while 1:
with open('shops.txt') as f:
for line in f.readlines():
line = line.strip()
commodity = line.split()[0]
price = line.split()[1]
products[commodity] = price
choice = raw_input("\n\033[;36mPlease enter what you want to buy,if you want back,you can enter\033[0m \033[;31mback\033[0m:").strip()
if len(choice) == 0:
continue
elif choice == 'back':
list()
#如果有这个商品,就判断商品价格,如果商品价格大于余额,就提示余额不足
if products.has_key(choice):
#从userinfo文件中读取并反序列化
remaining = pickle.load(open('userinfo','rb'))
if int(products[choice]) > remaining[accountAuth][2]:
print 'In your card remaining sum already insufficiency, please prompt sufficient value!'
else:
while 1:
#把购买的商品追加到购物车
shoplist.append(choice)
#计算余额,余额就是总金额减去购买的商品价格
new_remaining = int(remaining[accountAuth][2]) - int(products[choice])
userInfo[accountAuth][2] = int(new_remaining)
#把余额信息序列化并存到userinfo文件中
pickle.dump(userInfo,open("userinfo","wb"))
#把购买的记录和账单写到Bill文件中
Bill(accountAuth,time.strftime("%Y-%m-%d %H:%M:%S"),choice,"-%d" % int(products[choice]))
#打印消费的金额和剩余金额
print "\033[;32mConsumption is %r Money is %r\033[0m" % (products[choice],new_remaining)
#打印购物车的商品
print "\033[;33mThe shopping list %s \033[0m" % shoplist
break
else:
print 'You choose {} is not in the shoplist!'.format(choice)
shop()
#查询余额功能
def query_money():
userInfo = pickle.load(open('userin