day2、購物商城
阿新 • • 發佈:2017-05-07
else 能夠 rem exit 都是 login one 編號 余數
作業:購物商城
商品展示,價格
買,加入購物車
付款,錢不夠
代碼如下:
import codecs #登錄接口,用戶名密碼都正確登錄成功,否則失敗 def login(your_name,your_password): with open("user_file","r") as l: for line in l: user_name,user_password,user_balance=line.strip().split(":") if your_name == user_name and your_password == user_password: flag= 1 break else: print("用戶名或密碼錯誤") flag = 0 return flag #商品展示列表 def show_goods(): print(10*‘*‘ + "商品列表" + 10*"*") print(30 * "-") print("| 編號 | 名稱 | 價格 | 數量 | ") try: with codecs.open("goods_file","r","utf-8") as f1:for line in f1: (goods_id,goods_name,goods_rate,goods_num) = line.strip().split(":") print(30 * "-") print(‘| %s | %s | %s | %s |‘ %(goods_id,goods_name,goods_rate,goods_num)) print(30*"-") # print("購買商品請輸入產品編號/返回請輸入b/退出請輸入q") # the_num= input("請選擇:") except ValueError: pass #展示購物車 def show_gouwuche(your_name): with codecs.open("gouwuche","r","utf-8") as f2: n = 0 print(30 * "-") print("| 編號 | 名稱 | 數量 | 總價 |") for line in f2: user_name,goods_name,goods_num,goods_rate = line.strip().split(":") if user_name == your_name: n += 1 print(30 * "-") print(" | %s | %s | %s | %s |" %(n,goods_name,goods_num,goods_rate)) print(30*"-") #查看當前用戶已購產品 def show_yigou(your_name): with codecs.open("yigoushangpin","r","utf-8") as f3: n = 0 print(30*"-") print("| 編號 | 名稱 | 數量 | 總價 |") for line in f3: user_name,goods_name,goods_num,goods_rate = line.strip().split(":") if user_name == your_name: n += 1 print(30*"-") print(‘| %s | %s | %s | %s |‘ %(n,goods_name,goods_num,goods_rate)) print(30*"-") #賬戶余數查看 def show_money(your_name): with open("user_file","r") as l: for line in l: user_name,user_password,user_blance = line.strip().split(":") if your_name == user_name: print("您當前賬戶余額為:%s" %user_blance) break # 賬戶余額查看 def show_money(your_name): with open(‘user_file‘, ‘r‘) as l: for line in l: user_name, user_password, user_balance = line.strip().split(‘:‘) if your_name == user_name: print(‘您當前賬戶余額為:%s‘ % user_balance) break #加入購物車,如果購物車中已經存在該商品則將商品數量相加,商品價格相加;如果購物車中不存在該商品,則添加到購物車中,並將可購買的庫存量 def add_gouwuche(n,your_name): with codecs.open("goods_file","r+","utf-8") as f1: for line1 in f1: goods_id,goods_name,goods_rate,goods_num = line1.strip().split(":") if n == goods_id: goods_quantity = input("請輸入數量:") #如果輸入的並非數字則不合法,輸入的數量大於庫存,均需要重新輸入 while True: if goods_quantity.isdigit(): if int(goods_num) < int(goods_quantity): goods_quantity = input("您輸入的數量大於當前庫克數量,請重新輸入數量:") else: break else: goods_quantity = input("您輸入的不合法,請重新輸入數量:") with codecs.open("gouwuche","r+","utf-8") as f2: for line2 in f2: che_user_name,che_goods_name,che_goods_num,che_goods_rate = line2.strip().split(":") #判斷當前選擇的商品是否在購物車存在,如果存在標誌位che_flag = 1 if your_name == che_user_name and goods_name == che_goods_name: che_flag = 1 che_goods_num = str(int(che_goods_num) + int(goods_quantity)) che_goods_rate = str(int(che_goods_rate) + int(goods_rate)*int(goods_quantity)) old_line = line2 new_line = che_user_name + ":" + che_goods_name + ":" + che_goods_num + ":" + che_goods_rate + "\n" break #當前選擇的商品購物車中不存在,標誌位che_flag = 0 else: che_flag = 0 che_goods_rate = str(int(goods_rate) * int(goods_quantity)) che_goods_num = goods_quantity che_goods_name = goods_name che_user_name = your_name new_line = ("%s:%s:%s:%s\n" %(che_user_name,che_goods_name,che_goods_num,che_goods_rate)) #購物車中存在,則將購物車中的數量更新,總價格更新 if che_flag == 1: f2.seek(0) old_gouwuche = f2.read() new_gouwuche = old_gouwuche.replace(old_line,new_line) f2.seek(0) f2.truncate() f2.write(new_gouwuche) #購物車中不存在,則在購物車中添加 elif che_flag == 0: f2.seek(0) old_gouwuche =f2.read() new_gouwuche = old_gouwuche + new_line f2.seek(0) f2.truncate() f2.write(new_gouwuche) #加入購物車後,將商品列表中的數量更新 new_goods_num = str(int(goods_num) - int(goods_quantity)) old_goods_line = line1 new_goods_line = ("%s:%s:%s:%s\n" %(goods_id,goods_name,goods_rate,new_goods_num)) f1.seek(0) old_goods_file = f1.read() new_goods_file = old_goods_file.replace(old_goods_line,new_goods_line) f1.seek(0) f1.truncate() f1.write(new_goods_file) goods_flag = True break else: goods_flag = False print("抱歉您選擇的商品不存在,請重新選擇") return goods_flag #刪除購物車的商品 def del_gouwuche(del_gouwuche_num,your_name): with codecs.open("gouwuche","r+","utf-8") as f1: while True: n = 0 for line in f1: user_name,goods_name,goods_num,goods_rate = line.strip().split(":") #判斷刪除的商品是否存在,若存在撒謊拿出標誌位del_flag = 1 if user_name == your_name: n += 1 if int(del_gouwuche_num) == n: del_goods_name = goods_name del_goods_num = goods_num del_goods_line = line1 del_flag = 1 break #商品存在,進行刪除 if del_flag == 1: f1.seek(0) gouwuche_file = f1.readlines() gouwuche_file.remove(del_goods_line) f1.seek(0) f1.truncate() for line in gouwuche_file: f1.write(line) #更新商品清單,將從購物車刪除的加到商品清單中 with codecs.open("goods_file","r+","utf-8") as f2: for line2 in f2: old_goods_line = list(line2.strip().split(":")) old_line = line2.strip() if old_goods_line[1] == del_goods_name: old_goods_line[3] = str(int(old_goods_line[3])) + int(del_goods_num) new_goods_line = old_goods_line del_flag = 1 break if del_flag == 1: f2.seek(0) new_line = ":".join(new_goods_line) old_goods_file = f2.read() new_goods_file = old_goods_file.replace(old_line,new_line) f2.seek(0) f2.truncate() f2.write(new_goods_file) break #要刪除的商品不存在,標誌位del_flag = 0 else: del_flag = 0 print("您要刪除的商品不存在,請重新輸入編號!") break return del_flag #支付購物車商品 def pay_gouwuche(gouwuche_num,your_name): with codecs.open("gouwuche","r+","utf-8") as f1: while True: n = 0 for line1 in f1: user_name,goods_name,goods_num,goods_rate = line1.strip().split(":") #判斷刪除的商品是否存在,若存在刪除標誌位del_flag = 1 if user_name == your_name: n += 1 if int(gouwuche_num) == n: pay_goods_name = goods_name pay_goods_num = goods_num pay_goods_rate = goods_rate pay_goods_line = line1 #查看當前用戶余額是否能夠支付商品 with open("user_file","r+") as user_file: for line in user_file: user_name,user_password,user_balance = line.strip().split(":") if your_name == user_name: old_balance_line = line new_balance = int(user_balance) - int(goods_rate) break if new_balance < 0: print("您的當前預余額不足,請充值後再支付!\n") pay_flag = False break #大於等於0時,則可以支付,並更新賬戶余額 else: pay_flag = True new_balance_line = user_name + ":" + user_password + ":" + str(new_balance) + "\n" user_file.seek(0) old_user_file = user_file.read() new_user_file = old_user_file.replace(old_balance_line,new_balance.line) user_file.seek(0) user_file.truncate() user_file.write(new_user_file) break else: pay_flag = "" #商品存在,支付成功後進行刪除 if pay_flag == True: f1.seek(0) gouwuche_file = f1.readlines() gouwuche_file.remove(pay_goods_line) f1.seek(0) f1.truncate() for line in gouwuche_file: f1.write(line) #更新已購買的商品清單,將已支付的商品加入到購買的清單中 with codecs.open("yigoushangpin","r+","utf-8") as f2: for line2 in f2: old_yigou_line = list(line2.strip().split(":")) old_line = line2.strip() #當已購商品清單存在該商品購買記錄則將數量相加,總價格相加 if old_yigou_line[1] == pay_goods_name and old_yigou_line[0] == your_name: old_yigou_line[2] = str(int(old_yigou_line[2]) + int(pay_goods_num)) old_yigou_line[3] = str(int(old_yigou_line[3]) + int(pay_goods_rate)) new_yigou_line = old_yigou_line f2.seed(0) new_line = ":".join(new_yigou_line) old_yigou_file = f2.read() new_yigou_file = old_yigou_file.replace(old_line,new_line) f2.seed(0) f2.truncate() f2.write(new_yigou_file) break #當已購商品清單滅有購買記錄,則附加購買記錄 else: f2.seek(0) old_yigou_file = f2.read() new_yigou_file = old_yigou_file + pay_goods_line f2.seek(0) f2.truncate() f2.write(new_yigou_file) print("\n") print("恭喜你,支付成功!") break elif pay_flag == False: flag = True break #要刪除的商品不存在,標誌位del_flag = 0 else: pay_flag = False print("您要支付的商品不存在,請重新輸入編號!") break return pay_flag #賬戶充值 def add_money(blance_num,your_name): with open("user_file","r+") as f: for line in f: user_name,user_passwd,user_balance = line.strip().split(":") if user_name == your_name: old_user_line = line new_user_balance = str(int(user_balance) + int(balance_num)) new_user_line = user_name + ":" + user_passwd + ":" + new_user_balance + "\n" break f.seek(0) old_user_file = f.read() new_user_file = old_user_file.replace(old_user_line,new_user_line) f.seek(0) old_user_file = f.read() new_user_file = old_user_file.replace(old_user_line,new_user_line) f.seek(0) f.truncate() f.write(new_user_file) def main(): your_name = input("請輸入用戶名:") your_password = input("請輸入密碼:") menu_num = "" #登錄,用戶名或密碼錯誤重新輸入 while True: if login(your_name,your_password): print("歡迎%s登錄購物商城" %your_name) print(30 * "*") break else: your_name = input("請重新輸入用戶名:") your_password = input("請重新輸入密碼:") while True: print(30 * "*") print("歡迎來到主菜單,您可進行如下操作!") print(30 * "*") print("| 1 商品列表 | 2 查詢購物車 | 3 查看已購商品 | 4 賬戶余額 | q 退出商城 |") if menu_num == "": menu_num = input("請輸入:") #進入商品列表 elif menu_num == "1": while True: show_goods() print("| 請輸入商品編號加入購物車 | 輸入b返回主菜單 | 輸入q退出商城 |") your_goods_num = input("請選擇:") print("\n") #判斷輸入的是否為純數字,如果是 if your_goods_num.isdigit(): while add_gouwuche(your_goods_num,your_name): #當商品加入購物車後直接返回給購物車 menu_num = "2" break break elif your_goods_num == ‘b‘: menu_num = "" break elif your_goods_num == "q": print("您已退出商城!") else: print("您輸入的不合法,請重新輸入!\n") #查看購物車 elif menu_num == "2": while True: print(30 * "*") print("您當前購物車商品如下所示:") show_gouwuche(your_name) print("| 1 返回主菜單 | 2 刪除商品 | 3 支付商品 | q 退出商城 |") gouwuche_menu_num = input("請輸入:") print("\n") #返回主菜單 if gouwuche_menu_num == ‘1‘: menu_num = ‘‘ break #刪除商品 elif gouwuche_menu_num == ‘2‘: while True: del_gouwuche_num = input("請輸入要刪除的商品編號,或者輸入b返回:") if del_gouwuche_num == "b": break elif not del_gouwuche_num.isdigit(): print("您輸入的不合法,請重新輸入!\n") continue elif del_gouwuche(del_gouwuche_num,your_name): print("刪除成功\n") break #支付商品 elif gouwuche_menu_num == "3": while True: pay_gouwuche_num = input("請輸入要支付的商品編號,返回請輸入b:") if pay_gouwuche_num.isdigit(): if pay_gouwuche(pay_gouwuche_num,your_name): break elif pay_gouwuche_num == "b": break else: print("您輸入的不合法,請重新輸入!") elif gouwuche_menu_num == "q": print("您已退出商城!") exit() else: print("您輸入的不合法,請重新輸入\n") #查看已購買記錄 elif menu_num == "3": print(30 * "*") print("您已購買商品如下所示:") show_yigou(your_name) print("| 輸入b返回主菜單 | 輸入q退出程序 |") while True: yigou_input = input("請輸入:") if yigou_input == "b": menu_num = "" print("\n") break elif yigou_input == "q": print("您已退出商城!\n") else: print("你輸入的不合法,請重新輸入!\n") #查看賬戶信息 elif menu_num == "4": while True: print(30 * "-") print("您的賬戶信息如下所示:") show_money(your_name) print("| 1 充值賬戶 | b 返回主菜單 | q 退出商城 |") money_num == input("請輸入:") if money_num == "1": print(30 * "-") add_money_num = input("請輸入充值金額,輸入b返回:") if add_money_num.isdigit(): add_money_num = input("請輸入充值金額,輸入b返回:") if add_money_num.isdigit(): add_money(add_money,your_name) print("恭喜你,充值成功!\n") continue elif add_money_num == "b": continue else: print("您輸入的不合法,請重新輸入!\n") continue elif money_num == "b": menu_num = "" break elif money_num == "q": print("您已退出商城!") exit() else: print("您輸入的不合法,請重新輸入\n") elif menu_num == "q": print("您已退出商城!") exit() else: print("您的輸入不合法,請重新輸入!\n") if __name__ == ‘__main__‘: main()
上面代碼是網上找的,準備自己好好寫一遍,開始的時候沒有什麽思路,裏面主要是對文件的操作,還有一些新的方法,都是常用的方法,但是平時自己不會用的。
day2、購物商城