day2编写购物商城
作业:购物商城
商品展示,价格
买,加入购物车
付款,钱不够
流程图如下:
代码共有4个文件,如下:
用户文件:
alex 666 geng 888 zhang 222 lou 250 zeng 333
商品文件:
10001 小米3 2699 50 10002 比亚迪宋 100001 91 10003 格力变频空调 20000 4 10004 TCL电视 6000 98 10005 联想001 5600 992 10006 跑步鸡 250 663 10007 大众 58000 97 10008 马自达 68000 43
购物车文件:
10002 比亚迪宋 100001 1 geng 10006 跑步鸡 250 1 geng 10008 马自达 68000 1 geng 10003 格力变频空调 20000 1 geng
钱包文件:
geng 115880 zeng 126748
代码如下:
import sys def show(): '''商品展示模块''' shop_lists = [] shoppings = [] print("商品编号: 商品名称: 商品价格: 商品库存:", end='') print("\n*************************************************************************************") with open("shoppings","r") as f: for line in f: (shopping_num,shopping_name,shopping_price,shopping_stock) = line.strip().split() shop_lists.append(shopping_num) shoppings.append([shopping_num,shopping_name,shopping_price,shopping_stock]) print(shopping_num +" \t",shopping_name+" \t",shopping_price+" \t",shopping_stock+" \t") return (shop_lists,shoppings) def login(user): '''登录模块,用户验证用户登录状态''' users = {} with open("users_file","r") as user_f: for user_line in user_f: username,password = user_line.strip().split() users[username] = password if user in users.keys(): while True: pwd = input("请输入你的密码:") if pwd == users[user]: print("您好,欢迎%s,购物愉快!" %user) active = False return active else: print("对不起,您输入的密码不对,请重新输入!") else: print("对不起,您输入的账户不对,请核对后重新输入!") return True def shopping_cart(stock,user,stock_list,shoppings): '''购物车模块,向购物车中添加商品''' shop_lists = [] if stock in stock_list: while True: num = input("请输入你要购买商品的数量:") if int(num) <= int(shoppings[stock_list.index(stock)][3]): shop_lists.append([stock,shoppings[stock_list.index(stock)][1],shoppings[stock_list.index(stock)][2],num,user]) shoppings[stock_list.index(stock)][3] = str(int(shoppings[stock_list.index(stock)][3]) - int(num)) break elif int(num) > int(shoppings[stock_list.index(stock)][3]): print("对不起,您输入的数量超过库存,请重新输入!") else: print("对不起,您输入的商品编号不存在,请核对后输入!") with open("cart_file","a+") as append_f: for shop_list in shop_lists: user_line = " ".join(shop_list) append_f.write(user_line + "\n") return shoppings def balance(user): #计算此次购物消费 costs = [] with open("cart_file","r") as balance_f: for line in balance_f: num,name,price,stock,username = line.strip().split() if username == user: costs.append(int(price)*int(stock)) price = 0 for cost in costs: price += cost return price def recharge(user): '''充值模块''' money = input("请输入你要充值的金额:") user_wallets = [] with open("wallet_file","r") as charge_f: for line in charge_f: username,bal = line.strip().split() if username == user: bal = str(int(bal) + int(money)) user_wallets.append([username,bal]) else: user_wallets.append([username,bal]) with open("wallet_file","w") as recharge_f: for user_wallet in user_wallets: recharge_f.write(" ".join(user_wallet) + "\n") def account(user,price): #这里使用递归的方法来进行结算 user = user price = price wallets = [] with open("wallet_file","r") as obj_f: for line in obj_f: username,purse = line.strip().split() if username == user: if int(purse) >= int(price): purse = str(int(purse) - int(price)) print("支付成功!") wallets.append([username,str(purse)]) else: print("对不起,你的余额不足,请充值") recharge(user) account(username,purse) #递归执行函数,知道金额大于为止 else: wallets.append([username,str(purse)]) return wallets if __name__ == "__main__": active = True message = ''' ******************************************************************************************* \033[32;1m欢迎来到小猪猪购物网站,祝你购物愉快!\033[0m ******************************************************************************************* ''' print(message) lists = show() while active: user = input("请输入你的用户名:") active = login(user) global ShoppingStock global ShoppingLists ShoppingStock = lists[0] #定义全局变量,得到商品的编号,存在一个列表中,目的是获取商品的索引号 ShoppingLists = lists[1] #定义全局变量,存放遍历商品的信息,目的是方便之后写会文件 #购物车添加商品了 while True: shopping_index = input("请输入你要购买商品的编号(输入quit退出购物):") if shopping_index == "quit": #当用户输入quit时候,退出选购 break else: ShoppingLists = shopping_cart(shopping_index,user,ShoppingStock,ShoppingLists) with open("shoppings","w") as modify_f: for shopping_list in ShoppingLists: shopping_line = " ".join(shopping_list) + "\n" modify_f.write(shopping_line) price = balance(user) wallets = account(user,price) with open("wallet_file","w") as wallet_f: for user_line in wallets: wallet_f.write(" ".join(user_line) + "\n")
运行结果如下:
*******************************************************************************************
欢迎来到小猪猪购物网站,祝你购物愉快!
*******************************************************************************************
商品编号: 商品名称: 商品价格: 商品库存:
*************************************************************************************
10001 小米3 2699 50
10002 比亚迪宋 100001 92
10003 格力变频空调 20000 5
10004 TCL电视 6000 98
10005 联想001 5600 992
10006 跑步鸡 250 664
10007 大众 58000 97
10008 马自达 68000 44
请输入你的用户名:geng
请输入你的密码:888
您好,欢迎geng,购物愉快!
请输入你要购买商品的编号(输入quit退出购物):10002
请输入你要购买商品的数量:1
请输入你要购买商品的编号(输入quit退出购物):10006
请输入你要购买商品的数量:1
请输入你要购买商品的编号(输入quit退出购物):10008
请输入你要购买商品的数量:1
请输入你要购买商品的编号(输入quit退出购物):10003
请输入你要购买商品的数量:1
请输入你要购买商品的编号(输入quit退出购物):quit
对不起,你的余额不足,请充值
请输入你要充值的金额:100000
支付成功!
上述代码运行流程如下:
(1)展示商品信息;
(2)用户登录验证;
(3)用户输入想购买产品及数量,输入quit退出购物;
(4)添加到购物车文件;
(5)结算,去购物车计算购物花费;
(6)调用用户钱包文件,看余额是否够支付;
(7)余额大于等于等次购物,支付成功;余额不够,用户充值;
(8)调用充值模块,进行充值;
(9)递归,判断知道用户充值后的余额大于等于本次购物花费,支付成功;
(10)结束程序。
掌握知识:
(1)列表遍历的方法,列表修改是要先读取到一个列表中,然后根据用户输入的信息来进行修改;
(2)文件的来回打开与关闭;
(3)应用到了递归,就是当用户的余额一直较少的时候,就递归,直到用户的余额大于购物的花费为止;
(4)循环的开始与终止(break),程序的开始与终止(sys.exit),函数的开始与终止(return);
(5)列表的关联与索引情况,读取文件的顺序的解决问题。