1. 程式人生 > >購物車程序

購物車程序

更新 new log nco color read remove sde blog

購物車程序

要求:

- 有存放用戶信息的文件,三次登錄判斷用戶鎖定
- 有商品列表文件,歷史記錄問題,購物車文件
- 查看商品時要求分頁顯示,每頁顯示數量自定義
- 可以查看購物記錄

思路:

-----------------------------------------------------------------

購物車程序BUG:
1,程序執行完成後,文件沒有被更新;
2,在查看歷史記錄的時候,提示的當前頁碼數,顯示為0,不得不+1

3,沒有實現根據關鍵字搜索功能

代碼:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
#
讀取用戶信息 d1 = open(user_db, r, encoding=UTF-8) d2 = d1.read() d1.close() v1 = d2.split(\n) user_list = [] user_balance = [] for i in v1: v2 = i.split(|) user_list.append({ name: v2[0], pwd: v2[1], t: v2[2], balance: v2[3] }) flag = True while
flag: in_user = input(請輸入用戶名(q退出程序):) if in_user != q: for item in user_list: if in_user == item[name]: if int(item[t]) < 3: in_pwd = input(請輸入密碼:) if in_pwd == item[pwd]: item[
t] = 0 user_balance.append(item[balance]) print(登錄成功,您的賬戶余額為:%s % (item[balance])) flag = False break else: nt = 2 - int(item[t]) print(密碼錯誤,還剩%s嘗試次數 % nt) item[t] = int(item[t]) + 1 break else: print(此用戶已被鎖定) exit(程序退出) else: print(用戶不存在,請重新輸入:(q退出程序)) else: flag = False # 讀取商品文件信息 a1 = open(goods, r, encoding=UTF-8) a2 = a1.read() a1.close() # 讀取歷史文件信息 b1 = open(history, r, encoding=UTF-8) b2 = b1.read() b1.close() history_list = [] # 讀取購物車文件信息 c1 = open(cart, r, encoding=UTF-8) c2 = c1.read() c1.close() cart_list = [] # 將讀取到的商品列表信息轉換為一個列表 goods_list = [] goods_1 = a2.split(\n) for item_1 in goods_1: item_2 = item_1.split(:) goods_list.append({ name: item_2[0], price: item_2[1] }) # 判斷當前商品列表頁碼範圍 a = int(len(goods_list)) b = int(a % 10) c = a // 10 if b != 0: d = c + 1 else: d = c # 判斷當前歷史記錄頁碼範圍 e = int(len(history_list)) f = int(e % 10) g = e // 10 if f != 0: h = g + 1 else: h = g # 分頁顯示列表 flag = True while flag: choice = input(1:選購商品;2:刪除購物車商品;3:結算購物車商品;4:查看購買記錄;q:退出程序\n你的選擇是:) while flag: if choice == 1: x = input(請輸入頁碼查看商品列表(共%s頁;b返回;q退出): % (d,)) if x.isdecimal(): x = int(x) start = (x - 1) * 10 end = x * 10 goods_a = goods_list[start:end] goods_list_1 = [] for i in goods_a: goods_list_1.append(i) for j, k in enumerate(goods_list_1, 1): print(j, k) while flag: buy_choice = input(請選擇要購買的商品序號:) buy_choice = int(buy_choice) - 1 cart_list.append(goods_list_1[buy_choice]) print(已添加到購物車列表\n已選%s % (cart_list,)) break elif x == b: break elif x == q: print(程序退出) exit(bye) elif choice == 2: print(當前購物車商品:) for o, p in enumerate(cart_list, 1): print(o, p) cart_list_sum1 = 0 for k in cart_list: cart_list_sum1 += int(k[price]) print(當前購物車商品總金額%s: % (cart_list_sum1,)) del_choice = input(請選擇要刪除的商品序號(b返回):) if del_choice != b and int(del_choice) >= 0: del_choice = int(del_choice) - 1 cart_list.remove(cart_list[del_choice]) print(cart_list) else: print(輸入有誤) break elif choice == 3: cart_list_sum2 = 0 for k in cart_list: cart_list_sum2 += int(k[price]) history_list.append(k) print(當前購物車商品總金額:%s\n您的賬戶余額:%s % (cart_list_sum2, user_balance)) user_balance_int = int(user_balance[0]) if user_balance_int >= cart_list_sum2: user_balance_int -= cart_list_sum2 print(結算成功,當前余額:%s % (user_balance_int,)) break else: print(余額不足) break elif choice == 4: x1 = input(請輸入頁碼查看商品列表(共%s頁;b返回;q退出): % (h,)) if x1.isdecimal(): x1 = int(x1) start = (x1 - 1) * 10 end = x1 * 10 history_a = history_list[start:end] history_list_1 = [] for i1 in history_a: history_list_1.append(i1) for j1, k1 in enumerate(history_list_1, 1): print(j1, k1) #print(history_list) break elif choice == q: print(一路順風) exit(bye) else: print(抱歉,輸入有誤) break # 更新用戶登錄信息文件 user_list_new = [] for m in user_list: v3 = %s|%s|%s % (m[name], m[pwd], m[t]) user_list_new.append(v3) v4 = user_list_new[0] + \n + user_list_new[1] d3 = open(db, w) d3.write(v4) d3.close() # 更新歷史記錄文件 history_list_new = [] for n in history_list: b3 = %s:%s % (n[name], n[price]) history_list_new.append(b3) b4 = history_list_new[0] + \n + history_list_new[1] b5 = open(history, w) b5.write(b4) b5.close() # 更新購物車文件 cart_list_new = [] for o in cart_list: c3 = %s:%s % (o[name], o[price]) cart_list_new.append(c3) c4 = cart_list_new[0] + \n + cart_list_new[1] c5 = open(cart, w) c5.write(c4) c5.close()

--------

附商品列表信息文件goods

USP.45:500
DesertEagle 沙漠之鷹:650
M3 Super90 Combat:1700
AK47:2500
M4A1:3100
AUG A1:3500
ScoutSniperRifle:2750
AWP:4750
M249:5750
Glock18:400
SIG P228:600
M4 Super90 xm1014:3000
MP5 Navy:1500
Steyr TMP:1250
Fabrique Nationale:2350
UMP 45:3500
Mac 10 烏茲:1400
SG-552:3500
SIG 550:4200
Scout:3200

購物車程序