前言:这是初学时写的小项目,觉得有意思就写来玩玩,也当是巩固刚学习的知识。现在看来很不成熟,但还是记录一下做个纪念好了~

  1、名称网上网上银行及购物商城  

  2、项目结构

  当时刚接触python啦,哪里注意什么项目结构,就一脑子全塞到一个文件里面了

  

  •   代码全部在bank.py里面
  •   admin.txt记录所有用户的名字和密码(当时还不会用数据库)
  •   locked.txt记录被锁定用户的帐号
  •   huahua.pk是记录huahua这个用户的流水和消费情况

  3、效果:

  

  4、主要功能

  1.网上银行功能:

  •   能显示用户名与余额
  • 输入密码三次错误就锁定
  •   实现取钱,手续费3%
  •   将取钱记录写入账单中
  •   查看月流水
  •   每月的最后一天出账单

  2.购物商城功能:

  •   选购商品
  •   可以使用信用卡
  •   查看购物车
  •   购物车增加、删除商品
  •   结算扣款计入月账单

  5、设计过程

  5.1首先设置admin.txt中的用户名和密码

  5.2很骚气的设置打印出字体的颜色

# 设置打印字体的颜色
class change_color:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m' def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''

  5.3验证帐号的函数

  判断帐号是否存在、锁定、密码是否正确,三次密码不正确就锁定该用户(写入lock 文件)

# 判断帐号是否存在
def admin_is_exist(user_admin):
with open(admin_file, 'rb') as li:
for i in li.readlines():
i = i.strip().split()
if user_admin in i:
return True
else:
print '\n\t\t\t'+change_color.WARNING+'This admin is not exist! Please try again!'+change_color.ENDC
return False # 判断帐号是否被锁定
def admin_is_locked(user_admin):
with open(lock_file, 'rb') as lock:
for i in lock.xreadlines():
i = i.strip().split()
if user_admin in i:
print '\n\t\t\t'+change_color.WARNING+'This admin in locked! Please try another admin!'+change_color.ENDC
return False
else:
return True # 判断密码是否匹配
def password_is_match(user_admin, pass_word):
with open(admin_file, 'rb') as admin:
for i in admin.readlines():
i = i.strip().split()
if user_admin in i:
if pass_word == i[1]:
return True
else:
return False # 锁定用户
def lock_user(user_admin):
lock = open(lock_file, 'ab')
lock.write('\n' + user_admin)
lock.close()
print '\n\t\t\t'+change_color.WARNING+'Password do not match admin for 3 times! Admin is locked!'+change_color.ENDC

  5.4网上银行相关函数

  给每个用户建立一个以该用户名为标题的账单、写入月账单、写入并读取余额、取钱、打印用户流水

# 计算提现所需的余额(包括手续费)
def caculate_cash_with_fee(cash):
total = cash + cash * 0.05
return total # 计算取现后的余额
def balance_caculate(cash, balance):
total = balance - cash - cash * 0.05
return total # 初始化余额,如果有余额就不变,没有就初始化一个 (出错,文件为空)
def create_dict_in_balance():
filename = balance_file
if os.path.exists(filename):
if os.path.getsize(filename):
return 0
else:
fw = open(filename, 'wb')
user_balance = {'weiwei': 1520000, 'huahua': 52000, 'xiaoji': 100}
pickle.dump(user_balance, fw)
fw.close()
return 'new one' # 存储余额消息 # 每次初始化都替代,要改
def save_balance(user_admin, balance):
fr = open(balance_file, 'rb')
user_balance = pickle.load(fr)
user_balance[user_admin] = balance
fr.close()
fw = open(balance_file, 'wb')
pickle.dump(user_balance, fw)
fw.close() # 在每个用户的流水文件中存入一个新的名字相同的列表
def create_list_in_accout(user_admin):
filename = u'%s\\%s.pk' % BASE_DIR, user_admin
if os.path.exists(filename): # 如果文件存在
with open(filename, 'rb') as f:
li = pickle.load(f)
if 'accout_list' == li: # 列表存在,不改变
return 0
else: # 文件或列表不存在,创建
fw = open(filename, 'wb')
accout_list = []
pickle.dump(accout_list, fw)
fw.close()
return 'new list' # 存储用户流水 (序列化存储多个数据时最好使用列表等方式) # 要改成‘withdraw’和‘shop’两种形式,增加一个参数
def save_current_accout(user_admin, way, cash, balance):
current_accout = 'time: %s %s: %d balance: %d' % (time.strftime('%y-%m-%d %H:%M:%S'), way, cash, balance)
fr = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'rb')
accout_list = pickle.load(fr)
accout_list.append(current_accout) # 将新的流水写入列表
fr.close()
fw = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'wb')
pickle.dump(accout_list, fw)
fw.close()
return current_accout # 打印用户流水
def print_user_accout(user_admin):
f = open(u'%s\\%s.pk' % BASE_DIR, user_admin, 'rb')
accout_list = pickle.load(f)
f.close()
for i in accout_list:
print i # 读出某用户文档中的余额
def read_balance(user_admin):
f = open(balance_file, 'rb')
user_balance = pickle.load(f)
f.close()
return user_balance[user_admin] # 读出余额的字典
def read_balance_dict():
f = open(balance_file, 'rb')
user_balance = pickle.load(f)
f.close()
return user_balance

  5.5主函数

  购物这个功能是之后加的,所以直接在注函数实现,这样会让主函数十分冗余,不推荐。

  主函数就是一些基本的逻辑判断啊,人机交互之类的

# (全局变量)用户余额信息
shopping = {'orange': 30, 'milk': 50, 'bike': 200, 'lipstick': 350, 'bag': 3000, 'car': 100000, 'house': 1200000} # 主程序
if __name__ == '__main__':
print '\n\t\t\t\t'+change_color.HEADER+'Welcome to Internet Bank!'+change_color.ENDC # 欢迎界面
login = 1 # 为0表示已经登录进去了,为1表示尚未登录成功
while login: # 登录主程序
user_admin = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your admin name:'+change_color.ENDC) # 输入帐号
if admin_is_locked(user_admin): #判断帐号是否被锁定
if admin_is_exist(user_admin): #判断帐号是否存在
times = 3
while times:# 可以输入密码三次
pass_word = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your password:'+change_color.ENDC) # 输入密码
if password_is_match(user_admin, pass_word): # 密码正确,打印账户资料
print '\n\t\t\t\t'+change_color.HEADER+'Welcome!'+change_color.ENDC
login = 0
break
else:
times -= 1
print '\n\t\t\t'+change_color.WARNING+"Password do not match! You still have "+str(times)+" times to try!" + change_color.ENDC
if times == 0: # 输入密码三次不正确,锁定用户
lock_user(user_admin)
print '\n\t\t\t'+change_color.WARNING+"%s is locked " %user_admin + change_color.ENDC
sys.exit() # 网上银行界面
if login == 0:
create_list_in_accout(user_admin)
create_dict_in_balance()
user_interface = 1
while user_interface:
print '\n\t\t\t'+change_color.OKBLUE+'''
#################################################
user name: %s
user balance: %s Operation:
1.withdraw cash
2.shopping
3.month account
4.exit #################################################
''' % (user_admin, read_balance(user_admin)) + change_color.ENDC # 用户选择要进行的操作
operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4):'+change_color.ENDC) if operation == 1: # 提现操作
cash_interface = 1
while cash_interface:
cash = input('\n\t\t\t'+change_color.OKBLUE+'Please input cash you want:'+change_color.ENDC)
user_balance = read_balance_dict()
if user_balance[user_admin] >= caculate_cash_with_fee(cash): # 提现的钱不超过余额
if cash <= 15000: # 小于额度
balance = balance_caculate(cash, user_balance[user_admin]) # 计算新的余额
print '\n\t\t\t'+change_color.OKGREEN+'You can take your cash! Your card still have %s:' % balance + change_color.ENDC
# 将余额写入文档
save_balance(user_admin, balance)
# 将流水写入文档(做成函数)
save_current_accout(user_admin, 'withdraw cash', cash, balance)
else:
print '\n\t\t\t'+change_color.WARNING+'You can not take more than 15000!'+change_color.ENDC
else:
print '\n\t\t\t'+change_color.WARNING+'You balance is not enough!'+change_color.ENDC
# 选择是否继续
choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Do you want to continue?(y/n):'+change_color.ENDC)
if choice == 'y': # 继续提现
continue
else: # 返回主界面
cash_interface = 0 if operation == 2: # 购物车系统
buy_count = {} # 存放已经买的东西的种类
buy_counts = [] # 所有东西都直接加到购物车中
buy_balance = read_balance(user_admin)
can_buy = 1
while can_buy:
print '\n\t\t\t\t'+change_color.OKBLUE+'Welcome for shopping!'+change_color.ENDC
print
print '\n\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDC for i in enumerate(sorted(shopping.items(), key = lambda item:item[1]), 1): # 将字典中的元素转换成元组排序,编号并输出
print '\t\t\t\t'+change_color.OKBLUE+'%d %s %d'% (i[0], i[1][0], i[1][1]) + change_color.ENDC print '\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDC # 读取用户余额
print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC # 用户选择要进行的操作
buy = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to buy(1/2/3/4/...):'+change_color.ENDC)
shopping_list = sorted(shopping.items(), key=lambda item: item[1]) if buy_balance > shopping_list[buy - 1][1]: # 工资卡里的钱可以购买这样东西
# 把东西放到购物车里
buy_counts.append(shopping_list[buy - 1][0])
print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC
# 计算新的余额
buy_balance -= shopping_list[buy - 1][1]
else: # 钱不够, 使用信用额度
credit = raw_input( '\n\t\t\t'+change_color.WARNING+'Balance is not enough! Using credit(y/n)?'+change_color.ENDC)
if credit =='y':
# 把东西放到购物车里
buy_counts.append(shopping_list[buy - 1][0])
print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC
# 计算新的余额
buy_balance -= shopping_list[buy - 1][1]
choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) # 是否继续
if choice == 'y':
continue
if choice == 'n':
finish = 1
while finish:
print '\n\t\t\t'+change_color.OKBLUE+'''
################################################# Operation:
1.watch shopping car
2.add
3.reduce
4.accout
5.exit to user interface
6.exit system #################################################
''' + change_color.ENDC
# 用户选择要进行的操作
buy_operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4/5):'+change_color.ENDC) if buy_operation == 1: # 查看购物车
# 购物车列表转换为集合去重
buy_count = set(buy_counts)
buy_list = list(buy_count)
print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC # 打印选购的商品价格和数量
for i in enumerate(buy_list, 1):
print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) + change_color.ENDC
print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC # 打印总价
print '\n\t\t\t'+change_color.WARNING+ '10s later will come back to shopping operate interface' +change_color.ENDC
time.sleep(10) if buy_operation == 2: # 增加购物车里的东西
finish = 0 if buy_operation == 3: # 减去购物车里的东西
undo_finish = 1
while undo_finish:
# 读取用户余额
print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC
# 打印购物车
buy_count = set(buy_counts)
buy_list = list(buy_count)
print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC # 打印选购的商品价格和数量
for i in enumerate(buy_list, 1):
print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) + change_color.ENDC
print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC # 打印总价 # 用户选择要进行的操作
undo = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to reduce(1/2/...):'+change_color.ENDC) if buy_list[undo - 1] in buy_counts: # 如果商品在购物车中,删除
buy_counts.remove(buy_list[undo - 1])
print '\n\t\t\t'+change_color.OKGREEN+'%s is delete from you shopping car' % shopping_list[undo - 1][0] +change_color.ENDC
# 计算新的余额
buy_balance += shopping[buy_list[undo - 1]]
else: # 购物车里没有该物品
print '\n\t\t\t'+change_color.WARNING+'%s is not in you shopping car' % buy_list[undo - 1]+change_color.ENDC undo_choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) # 是否继续
if undo_choice == 'y':
continue
if undo_choice == 'n':
undo_finish = 0 if buy_operation == 4: # 结算
buy_count = set(buy_counts)
buy_list = list(buy_count)
print '\n\t\t\t\t'+change_color.OKBLUE+'name price amount' + change_color.ENDC # 打印选购的商品价格和数量
for i in buy_list:
print '\t\t\t\t'+change_color.OKBLUE+'%s %d %d'% (i, shopping[i], buy_counts.count(i)) + change_color.ENDC
total = read_balance(user_admin) - buy_balance
print '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % total +change_color.ENDC # 打印总价 pay = raw_input('\n\t\t\t'+change_color.OKGREEN+'Do you want to pay(y/n)?'+change_color.ENDC)
if pay == 'y': # 确认付款,将流水和余额写入文件
# 余额
save_balance(user_admin, buy_balance)
print '\n\t\t\t'+change_color.OKGREEN+'Successful payment!'+change_color.ENDC
print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC
# 流水写入文件
save_current_accout(user_admin, 'shopping', total, buy_balance)
finish = 0 # 返回主界面
can_buy = 0
time.sleep(3)
if pay == 'n': # 如果不付款,返回商品操作界面
can_buy = 0 if buy_operation == 5: # 退回主界面
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC
finish = 0
can_buy = 0 if buy_operation == 6: # 退出
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC
sys.exit() if not (buy_operation == 1 or buy_operation == 2 or buy_operation == 3 or buy_operation == 4 or buy_operation == 5 or buy_operation == 6):
print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC
time.sleep(3) if operation == 3: # 查看流水账操作 #'dict' object has no attribute 'readline
print_user_accout(user_admin)
print '\n\t\t\t'+change_color.WARNING+'10s later will come back to user interface'+change_color.ENDC
time.sleep(10) if operation == 4: # 退出系统
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for using Internet Bank!'+change_color.ENDC
sys.exit() if not (operation == 1 or operation == 2 or operation == 3 or operation == 4):
print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC
time.sleep(3)

  整个系统的逻辑其实很简单,说一下我在这个过程中遇到的问题吧。

  由于我刚刚接触这个语言,代码写的非常不整洁,冗余部分很多(我的主函数太长了,长到我自己都有点懵),其实可以把很多部分单独拎出来作为一个函数,这样整个项目的可读性比较好。还有很多不足之处,一时也说不上来,等我回去想想~

  我会把整个项目贴在我的github上,欢迎大家来提供意见~

  完整项目代码 :https://github.com/huahua462/bank-sopping

Python实战之网上银行及购物商城的更多相关文章

  1. 洗礼灵魂,修炼python(81)--全栈项目实战篇(9)—— 购物商城登录验证系统

    都在线购物过吧?那么你应该体验过,当没有登录账户时,点开购物车,个人中心,收藏物品等的操作时,都会直接跳转到登录账户的界面,然后如果登录一次后就不用再登录,直到用户登出. 是的,本次项目就是做一个登录 ...

  2. Python开发程序:ATM+购物商城

    一.程序要求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还 ...

  3. Python实现ATM+购物商城

    需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...

  4. Python学习笔记-练习编写ATM+购物车(购物商城)

    作业需求: 模拟实现一个ATM + 购物商城程序: 1.额度 15000或自定义 2.实现购物商城,买东西加入 购物车,调用信用卡接口结账 3.可以提现,手续费5% 4.支持多账户登录 5.支持账户间 ...

  5. python day19 : 购物商城作业,进程与多线程

    目录 python day 19 1. 购物商城作业要求 2. 多进程 2.1 简述多进程 2.2 multiprocessing模块,创建多进程程序 2.3 if name=='main'的说明 2 ...

  6. python 信用卡系统+购物商城见解

    通过完成信用卡系统+购物商城 使自己在利用 字典和列表方面有了较大的提升,感悟很深, 下面将我对此次作业所展示的重点列表如下: #!/usr/bin/env python3.5 # -*-coding ...

  7. Python 实现购物商城,含有用户入口和商家入口

    这是模拟淘宝的一个简易的购物商城程序. 用户入口具有以下功能: 登录认证 可以锁定用户 密码输入次数大于3次,锁定用户名 连续三次输错用户名退出程序 可以选择直接购买,也可以选择加入购物车 用户使用支 ...

  8. Python实战之ATM+购物车

    ATM + 购物车 需求分析 ''' - 额度 15000或自定义 - 实现购物商城,买东西加入 购物车,调用信用卡接口结账 - 可以提现,手续费5% - 支持多账户登录 - 支持账户间转账 - 记录 ...

  9. 商城项目实战 | 1.1 Android 仿京东商城底部布局的选择效果 —— Selector 选择器的实现

    前言 本文为菜鸟窝作者刘婷的连载."商城项目实战"系列来聊聊仿"京东淘宝的购物商城"如何实现. 京东商城的底部布局的选择效果看上去很复杂,其实很简单,这主要是要 ...

随机推荐

  1. mysql FULL JOIN关键字 语法

    mysql FULL JOIN关键字 语法 作用:只要其中某个表存在匹配,FULL JOIN 关键字就会返回行.“富瑞华”牌大理石构件 语法:SELECT column_name(s) FROM ta ...

  2. Bellman-ford算法、SPFA算法求解最短路模板

    Bellman-ford 算法适用于含有负权边的最短路求解,复杂度是O( VE ),其原理是依次对每条边进行松弛操作,重复这个操作E-1次后则一定得到最短路,如果还能继续松弛,则有负环.这是因为最长的 ...

  3. [LightOJ1240]Point Segment Distance 题解

    题意简述 原题LightOJ 1240,Point Segment Distance(3D). 求三维空间里线段AB与C. 题解 我们设一个点在线段AB上移动,然后发现这个点与原来的C点的距离呈一个单 ...

  4. Ubuntu 16.04下使用docker部署ceph集群

    ceph集群docker部署 通过docker可以快速部署小规模Ceph集群的流程,可用于开发测试. 以下的安装流程是通过linux shell来执行的:假设你只有一台机器,装了linux(如Ubun ...

  5. Dancing Stars on Me

    Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  6. 使用java操作elasticsearch之与es简历连接

    Elasticsearch 的 Java 客户端非常强大:它可以建立一个嵌入式实例并在必要时运行管理任务. 运行一个 Java 应用程序和 Elasticsearch 时,有两种操作模式可供使用.该应 ...

  7. 关于VMware 15搭建MacOS 10.14后无法播放在线视频和客户端视频的问题

    最近在自己的电脑上搭建了MacOS10.14系统,搭建是成功了,但是发现一个很坑的事,看视频发现黑屏.就是那种只有声音,没有视频的问题,在多个浏览器上和客户端都是一样的.百度了下,总结有2种可能,一是 ...

  8. 使用OpenLayers 勾画地图

    <!DOCTYPE html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta charset= ...

  9. 小白的git克隆流程clone

    首先你进去你要存放代码的位置,比如将代码存放到D盘,然后在D盘中右键,点击Git Bash Here,就是说本地仓库要在D盘建立. 然后出现git 命令行界面,然后输入命令:git clone + 远 ...

  10. 单页应用 cookies处理

    w Node & 单页应用 来做一个完整用户系统吧! - harryfyodor的前端专栏 - SegmentFaulthttps://segmentfault.com/a/119000000 ...