乙個很low的購物車系統還花了我一下午時間

2022-05-07 12:51:12 字數 3580 閱讀 1699

購物車的要求

有一兩個入口

使用者入口:

1.商品資訊存在檔案裡

2.已購商品餘額記錄

商家入口

2.可新增商品,修改商品

使用者入口

倆個檔案乙個product.txt商品資訊   乙個歷史購買資料

使用者輸入本想加乙個判斷歷史購買是否由餘額然後直接輸入餘額  ,但是if判斷好像返回不了返回值

1

#author:zhiyu su

23 shopping_list=

4 f=open('

product.txt

','r

',encoding='

utf-8')

5 product_list=eval(f.read())

67 l = open('

歷史購買資料.txt

', '

r', encoding='

utf-8

').read()89

1011 salary =l

12print('

以前購物餘額

',salary)

1314 salary = input("

input your salay")

15if salary.isdigit():#

字串形式的數字返回為真

16 salary =int(salary)

17while

true:

18for index,item in enumerate(product_list):#

enumerate 取元素的下標

1920

#print(product_list.index(item),item)

21print

(index,item)

22 user_choice = input("

選擇》")23

ifuser_choice.isdigit():

24 user_choice=int(user_choice)

25if user_choice < len(product_list) and user_choice >=0:

26 p_item =product_list[user_choice]

27if p_item[1] <= salary:#

買的起28

29print

(shopping_list)

30 salary-= p_item[1]

31print("

added%s into shopping cart your crent balance is \033[31;1m%s\033[0m

" %(p_item,salary)) #

字型紅色 \033[31;1m**\033[0m

32else:33

print("

\033[41;1m你的餘額只剩[%s]啦,還買個毛線\033[0m

"%salary)

34else:35

print("

不存在[%s]

"%user_choice)

36elif user_choice == 'q'

:37print("

-----------shopping list--------------")

38for p in

shopping_list:

39print

(p)40

print("

your current balance:

",salary)

41 k = open('

歷史購買資料.txt

', '

w', encoding='

utf-8')

42k.write(str(salary))

43k.write(str(p))

44k.close()

45exit()

46else:47

print("

sojdioj")

48 f.close()

商家入口

有待完善的地方:修改完畢之後儲存兩個檔案乙個是使用文件,乙個是備份。文件以w模式寫   而備份以追加模式寫再加每次存寫的日期 ,如果文件丟失按最接的時間拷貝到使用文字裡(這裡配備時間可能需要用到正則 )

1

#author:zhiyu su23

456 f = open('

product.txt

','r

',encoding='

utf-8')

789 data =eval(f.read())

10 f_new = open('

product.txt

','w

',encoding='

utf-8')

11#for i in data:12#

print(i)

1314

for index,item in enumerate(data):#

enumerate 取元素的下標

15print

(index,item)

16 choice = input('

你要修改商品按x你要新增商品按t>>>>')

17if choice =='x'

:18 choice = int(input("

選擇要修改的商品號》"))

19if choice 20print

(data[choice])

21 choice2 = input('

修改名稱按--m--還是修**按--q-->>>')

22if choice2 =='m'

:23 choice3 = input("

你要修改的商品名稱》")

24 data[choice][0]=choice3

25print

(data)

26f_new.write(str(data))

27elif choice2 == 'q'

:28 choice4 = input("

你要修改的商品名稱》")

29 data[choice][1] =choice4

30print

(data)

31f_new.write(str(data))

32elif choice == '

t': #

新增功能

商品名稱》')

34 end = input('

商品**》')

3536

f_new.write(str(data))

37else:38

print('

兄弟只有t和x')

39f.close()

40 f_new.close()

乙個PHP購物車類

cart 購物車類 author doodoo package cart category cart license php license access public version revision 1.10 class cart reset item 這一句是必須的,因為上面的判斷已經移動了陣...

乙個php購物車的例子

網上搜到的,簡單容易理解。cookie存購物車id,db存購物車資料。購物車session的產生 if session scid class cart 查詢失敗 numrows mysql num rows result if numrows 0 else function add item tab...

php 實現乙個簡易的購物車

通過提交資料,使用php來出里購物車商品的價錢計算。實現方式 php陣列遍歷。總的過程就是 1 表單基本功能 資料可加減 2 表單資料的處理,遍歷三個資料,獲取字串 3 在php中將字串轉化成陣列,才能計算,第一步 前台資料的處理 在中建立乙個商品基本資訊 針對數量部分,在jq物件裡實現加減 fun...