python购物车小程序
购物车程序
需求:
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
实现了一些附加功能,生成一些文件,当然基于Python的基础实现的,高大上的暂时不会,基本上靠累代码.....借鉴了一些,谢谢!不如他们写的。
http://www.cnblogs.com/wushank/p/5087502.html
http://www.cnblogs.com/spykids/p/5163108.html
#!/usr/bin/env python #-*- encoding:utf8 -*- import getpass import os def env_peizhi(): count = 0 user_status = os.path.isfile("user_list") if user_status == False: user_list = open("user_list","w") user_list.close() status = os.path.isfile("black_list") if status == False: black_user = open("black_list","w") black_user.close() b_user = open("black_list","r") B_user = b_user.read() b_user.close() def zhuce(): #验证是否在黑名单 black = {} b = open('black_list','r') d = b.readlines() for i in d: b_u = i.split('\n')[0] black[b_u] = '' b.close() #验证是否是注册用户 user = {} f = open('user_list','r') d = f.readlines() for i in d: u = i.split(':')[0] user[u] = '' f.close() #登陆判断 for i in range(3): n = 0 useradd = input("请输入用户名:").strip() #success_1 = "成功创建用户:%s" %(useradd) b_rs = useradd in black u_rs = useradd in user len_user = len(useradd) if len_user == 0: print("用户名为空,请重新输入") n = n + 1 continue elif str(b_rs) == 'True': print("已注册已被锁定,请重新输入用户名或退出") choice_b = "1.注册 q.退出" ch_b = choice_b.center(32,'*') print(ch_b ) n = n + 1 for i in range(3): sr_b = input("请输入选项:") p = 0 if sr_b == 'q': tuichu() elif sr_b == '1': zhuce() else: print("请输入选项:1.注册 q.退出") p = p + 1 continue else: print("输入错误次数太多,请稍后再试") tuichu() elif str(u_rs) == 'True': print('用户名已注册,请重新输入用户名或退出') choice_2 = "1.注册 q.退出" ch_2 = choice_2.center(32,'*') print(ch_2 ) n = n + 1 for i in range(3): sr_a = input("请输入选项:") p = 0 if sr_a == 'q': tuichu() elif sr_a == '1': break else: print("请输入选项:1.注册 q.退出") p = p + 1 continue else: print("输入错误次数太多,请稍后再试") tuichu() else: n = 0 pass passwd = input("请输入密码:") length = len(passwd) if length == 0 : print("密码为空,请重新输入") n = n + 1 continue else: #break w_1 = '%s:%s:0\n' %(useradd,passwd) #print(w_1) user_list_now = open("user_list","a") user_list_now.write(w_1) user_list_now.close() print("注册成功") login() else: print("尝试次数太多,请稍后再试") tuichu() def tuichu(): bye = "欢迎再次光临XX商城" bye_1 = bye.center(30,'*') print('\n') print(bye_1) exit() def login(): #os.system('clear') print("欢迎来到XX商城,请选择") choice_1 = "1.登陆 2.注册 q.退出" ch_1 = choice_1.center(32,'*') print(ch_1 ) for i in range(3): s = 0 sr_1 = input("请输入选项:") if len(sr_1) == 0: print("输入为空,请重新输入") s = s + 1 continue elif sr_1 == 'q': tuichu() elif sr_1 == '1': denglu() elif sr_1 == '2': zhuce() else: print("输入错误,1.登陆 2.注册 q.退出") s = s + 1 else: print("输入次数太多,请稍后再试") tuichu() def denglu(): black = {} b = open('black_list','r') d = b.readlines() for i in d: b_u = i.split('\n')[0] black[b_u] = '' b.close() user = {} f = open('user_list','r') d = f.readlines() for i in d: u = i.split(':')[0] user[u] = '' f.close() e = 0 for i in range(3): input_name = input("请输入登陆用户名:").strip() b_rs = input_name in black u_rs = input_name in user if len(input_name) == 0: print("用户名为空,请重新输入") e = e + 1 continue elif str(b_rs) == 'True': #print(b_rs) print("用户被锁定,请联系管理员或使用其他用户名") choice_3 = "1.登陆 2.注册 q.退出" ch_3 = choice_3.center(33,'*') print(ch_3 ) g = 0 for i in range(3): sr_2 = input("请输入选项:") if sr_2 == 'q': tuichu() elif sr_2 == '1': g = g + 1 denglu() elif sr_2 == '2': g = g + 1 zhuce() elif len(sr_2) == 0: print("输入为空,请重新输入") g = g + 1 continue else: print("请输入选项 1.登陆 2.注册 q.退出") g = g + 1 continue else: print("输入错误次数太多,请稍后再试") tuichu() elif str(u_rs) == 'False': #print(u_rs) print('用户名未注册,请用其他用户登录,注册或退出') choice_2 = "1.注册 q.退出" ch_2 = choice_2.center(32,'*') print(ch_2 ) for i in range(3): g = 0 sr_2 = input("请输入选项:") if sr_2 == 'q': tuichu() elif sr_2 == '1': zhuce() else: print("请输入选项.1注册 q.退出") g = g + 1 continue else: print("输入错误次数太多,请稍后再试") tuichu() elif str(u_rs) == 'True': o = open("user_list","r") t = o.readlines() o.close() for uline in t: user,password,mony = uline.strip().split(':') if user == input_name: for i in range(3): c = 0 #input_pwd = getpass.getpass("请输入密码:") input_pwd = input("请输入密码:") if input_pwd == password: if os.path.isfile('goods') == False : print("懵逼了,没有商品,白来一趟,退出啦!") tuichu() else: h = open('goods','r') f = h.read() now_money=mony name = input_name if os.path.isfile('history') == False: shoppinglist_dict = {} #shoppinglist_dict[name] = [] shoppinglist_dict = str(shoppinglist_dict) f_1 = open('history','w') f_1.write(shoppinglist_dict) f_1.close() shoppinglist_dict = {} else: file_list_r = open('history','r+',encoding='utf-8') f_list_r = str(file_list_r.read()) file_list_r.close() shoppinglist_dict = eval(f_list_r) if name not in shoppinglist_dict: shoppinglist_dict[name] = [] shoppinglist_dict = str(shoppinglist_dict) m_1 = open('history','w') m_1.write(shoppinglist_dict) m_1.close() shoppinglist_dict = eval(shoppinglist_dict) #print("-----------------------------------------------------------") #print(shoppinglist_dict) #print(shoppinglist_dict[name]) bbb = (''' ********************************************************************************* * * * 欢迎来到sky购物平台 * * * ********************************************************************************* | 会员:%s 当前余额:%s * --------------------------------------------------------------------------------- 编号 名称 售价 * --------------------------------------------------------------------------------- %s --------------------------------------------------------------------------------- ''') %(input_name,now_money,f) print(bbb) while True: print("1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出\n") choose = input("请输入选项:\t").strip() # 询问是否需要查询历史记录 if choose == '1': print("----------------------------") result=[] fd = open( "goods", "r" ) for line in fd.readlines(): l=list(line.split()) result.append(l) print("编号 名称 售价 ") c = [] for index,item in enumerate(result,0): print(index,item) c.append(index) shop_list_now = [] shop_list_pr = [] goods_money = 0 goods_money = int(goods_money) while True: #print(shop_list) choice_11 = input("输入数字选择商品,退出请输入q,返回上一层b:\t").strip() if choice_11.isdigit(): choice_11 = int(choice_11) if (choice_11 in c) == False : print("编号错误,不在商品库中,请重新输入.") break else: # print(choice_11) goods_price = result[choice_11][1] goods = result[choice_11][0] goods_price = int(goods_price) now_money = int(now_money) if goods_price < now_money: shop_list_now.append(result[choice_11]) shop_list_pr.append(result[choice_11]) print("---添加购物车---") #print("------------------11111-----------------------------------") #print(shop_list_now) #print("------------------22222-----------------------------------") #print(type(shoppinglist_dict[name])) #print("------------------33333-----------------------------------") shoppinglist_dict[name].extend(shop_list_now) #print(shoppinglist_dict[name]) m = open('history','w') shoppinglist_dict = str(shoppinglist_dict) #print(shoppinglist_dict) m.write(shoppinglist_dict) shoppinglist_dict = eval(shoppinglist_dict) m.close() shop_list_now = [] now_money = now_money - goods_price goods_money = goods_money + goods_price print("-------------------------") print("您购买了%s,价格是%s,余额是%s" %(result[choice_11][0],result[choice_11][1],now_money)) print("--------------------------") now_money = str(now_money) mony = str(mony) uline = "%s:%s:%s" %(name,password,mony) uline = str(uline) p = "%s:%s:%s" %(name,password,now_money) p = str(p) #print(p) now_2 = open("user_list","r") v = now_2.readlines() now_2 = open("user_list","w") for i in v: now_2.write(i.replace(uline,p)) now_2.close() mony = now_money else: print("您的账户余额不足,重新选择商品或返回上一层充值b:") if choice_11 == 'b': break elif choice_11 == 'q' : print("您购买了%s."%(shop_list_pr)) tuichu() elif choice_11 == 'b': break else: print("你是SB吗?") elif choose == '2': # 选y,输出历史购物记录 m_2 = open('history','r') m_3 = str(m_2.read()) shoppinglist_dict = eval(m_3) m_2.close() print("-----------历史购物记录------------") print(shoppinglist_dict[name]) print("--------------结束---------------\n") elif choose == '3': while True: #print(now_money) now_money = int(now_money) print(("您的账户余额是:%s.") %(now_money)) cz_money = input("请输入充值金额,返回上一层请输入b:") if cz_money.isdigit(): cz_money = int(cz_money) now_money = now_money + cz_money print(("充值成功,充值%s.") %(cz_money)) t = open('user_list','r+') c = t.readlines() t = open('user_list','w') x = "%s:%s:%s" %(name,password,mony) x = str(x) y = "%s:%s:%s" %(name,password,now_money) y = str(y) #print("start-----更改余额----") for d in c: t.write(d.replace(x,y)) mony = now_money t.close() #print("stop-----更改余额----") elif cz_money == 'b': break else: print("输入错误,请输入数字") elif choose == '4': login() elif choose == 'q': print("\n") tuichu() else: print("请重新输入:---眼睛睁大点----") else: print("密码错误,请重新输入。") c = c + 1 else: print("对不起,您输入的密码错误次数已达3次,将锁定您的账户!") f = open("black_list","a") f.write("%s\n" %(input_name)) f.close() tuichu() else: print("输入错误次数太多,请稍后再试") tuichu() env_peizhi() login()
[admin@pe-jira python]$ python buy.py
欢迎来到XX商城,请选择
*********1.登陆 2.注册 q.退出*********
请输入选项:33
输入错误,1.登陆 2.注册 q.退出
请输入选项:d
输入错误,1.登陆 2.注册 q.退出
请输入选项:f
输入错误,1.登陆 2.注册 q.退出
输入次数太多,请稍后再试
**********欢迎再次光临XX商城**********
[admin@pe-jira python]$ python buy.py
欢迎来到XX商城,请选择
*********1.登陆 2.注册 q.退出*********
请输入选项:1
请输入登陆用户名:33
用户名未注册,请用其他用户登录,注册或退出
***********1.注册 q.退出************
请输入选项:1
请输入用户名:33
请输入密码:33
注册成功
欢迎来到XX商城,请选择
*********1.登陆 2.注册 q.退出*********
请输入选项:1
请输入登陆用户名:33
请输入密码:1
密码错误,请重新输入。
请输入密码:2
密码错误,请重新输入。
请输入密码:3
密码错误,请重新输入。
对不起,您输入的密码错误次数已达3次,将锁定您的账户!
**********欢迎再次光临XX商城**********
[admin@pe-jira python]$ python buy.py
欢迎来到XX商城,请选择
*********1.登陆 2.注册 q.退出*********
请输入选项:1
请输入登陆用户名:33
用户被锁定,请联系管理员或使用其他用户名
**********1.登陆 2.注册 q.退出*********
请输入选项:1
请输入登陆用户名:11
请输入密码:11
懵逼了,没有商品,白来一趟,退出啦!
**********欢迎再次光临XX商城**********
[admin@pe-jira python]$
[admin@pe-jira python]$ python buy.py
欢迎来到XX商城,请选择
*********1.登陆 2.注册 q.退出*********
请输入选项:1
请输入登陆用户名:11
请输入密码:11
*********************************************************************************
* *
* 欢迎来到sky购物平台 *
* *
*********************************************************************************
| 会员:11 当前余额:0 *
---------------------------------------------------------------------------------
编号 名称 售价 *
---------------------------------------------------------------------------------
IPhone 6600
MACbook 8000
Xiaomi 1999
OPPO 2799
Coffee 35
TESLA 870000
BMW_car 340000
bike 80
---------------------------------------------------------------------------------
1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出
请输入选项: 2
-----------历史购物记录------------
[]
--------------结束---------------
1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出
请输入选项: 3
您的账户余额是:0.
请输入充值金额,返回上一层请输入b:333333333
充值成功,充值333333333.
您的账户余额是:333333333.
请输入充值金额,返回上一层请输入b:1111111
充值成功,充值1111111.
您的账户余额是:334444444.
请输入充值金额,返回上一层请输入b:b
1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出
请输入选项: 1
----------------------------
编号 名称 售价
0 ['IPhone', '6600']
1 ['MACbook', '8000']
2 ['Xiaomi', '1999']
3 ['OPPO', '2799']
4 ['Coffee', '35']
5 ['TESLA', '870000']
6 ['BMW_car', '340000']
7 ['bike', '80']
输入数字选择商品,退出请输入q,返回上一层b: 0
---添加购物车---
-------------------------
您购买了IPhone,价格是6600,余额是334437844
--------------------------
输入数字选择商品,退出请输入q,返回上一层b: 1
---添加购物车---
-------------------------
您购买了MACbook,价格是8000,余额是334429844
--------------------------
输入数字选择商品,退出请输入q,返回上一层b: b
1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出
请输入选项: 2
-----------历史购物记录------------
[['IPhone', '6600'], ['MACbook', '8000']]
--------------结束---------------
1.购物 2.历史购物记录 3.充值 4.注销当前用户 q.退出
请输入选项: q
**********欢迎再次光临XX商城**********
[admin@pe-jira python]$