作業:購物商城 商品展示,價格 買,加入購物車 付款,錢不夠 流程圖如下: 代碼共有4個文件,如下: 用戶文件: 商品文件: 購物車文件: 錢包文件: 代碼如下: 上述代碼運行流程如下: (1)展示商品信息; (2)用戶登錄驗證; (3)用戶輸入想購買產品及數量,輸入quit退出購物; (4)添加到 ...
作業:購物商城
商品展示,價格
買,加入購物車
付款,錢不夠
流程圖如下:
代碼共有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)列表的關聯與索引情況,讀取文件的順序的解決問題。