使用裝飾器實現購物車的日誌記錄

2021-10-10 00:24:57 字數 2789 閱讀 1196

# __author: xy

# __data: 2020/9/29

import time

choice_car =

moneys =

90000

money =

0re = moneys

li =[(

'bike'

,900),

('fly'

,800),

('car'

,900)]

definital_vale()

:for i, v in

enumerate

(li,0)

: m, n = v

print

(i, m, n)

return

true

defbuy_log

(f):

defprint_log

(m, choice=):

with

open

('buy.log'

,'a'

)as shopping:

shopping.write(time.strftime(

'%y-%m-%d %h-%m-%s')+

'\n'

)for ch in choice:

x, y = choice[ch]

shopping.write(

'貨號:'

+ ch +

'--'

+'單價:'

+str

(x)+

'--'

+'數量:'

+str

(y)+

'\n'

) f(choice)

shopping.write(

'餘額:'

+str

(m)+

'\n'

) shopping.write(time.strftime(

'%y-%m-%d %h-%m-%s')+

'\n'

)return print_log

@buy_log

defbuy

(m, choice=):

choice.clear(

)return

true

defcar

(*args)

:global choice_car, moneys

while

true

: a =

0global re

inital_vale(

) choice =

input

('請輸入商品的編號(q退出j結算):'

)if choice.isdigit(

)and

int(choice)

<

len(li)

: num =

input

('請輸入需要的數量:'

)if num.isdigit():

x, y = li[

int(choice)

]print

("你選擇的結果:"

, x, y, num,

int(num)

* y)

print

(x,[y,

int(num)])

choice_car.update(

dict((

(x,(y,

int(num)))

,)))

# print(choice_car)

for item in choice_car.items():

print

('商品金額:'

, item[0]

, num, item[1]

[0]* item[1]

[1])

a += item[1]

[0]* item[1]

[1] re = moneys - a

print

('商品總額:'

, a)

print

('賬戶餘額:'

, re)

else

:print

('請輸入整數!'

)elif choice ==

'j':

if re >=0:

moneys = re

buy(moneys, choice_car)

choice_car.clear(

)print

('購買成功,你的賬戶餘額是:'

, moneys)

continue

else

: recharge =

input

('賬戶餘額不足,請充值!'

.format

(re=re)

) re +=

int(recharge)

print

('賬戶餘額:'

, re)

continue

elif choice ==

'q':

break

else

:print

('你輸入的資訊不存在,請重新輸入!'

)return

true

if __name__ ==

'__main__'

: car(re)

使用Redis實現購物車

cart.class.php 使用redis實現乙個購物車功能 class cart public function addtocart gid,cartnum 1 根據商品id查詢商品資料 gooddata this goodsdata gid key cart session id gid id...

php使用cookie實現購物車

檢視 原始碼時 看到的做下筆記 檔案包括 test.php,cart.php 一.test.php 商品顯示頁面 cart list empty cookie cart list unserialize cookie cart list array 例子使用陣列實際會用mysql product l...

使用Selector API實現購物車客戶端計算

html 商品名稱 單價數量小計 iphone6 4488.00 1 4488.00 iphone6 plus 5288.00 1 5288.00 ipad air 2 4288.00 1 4288.00 total 14064.00 css table td,th td colspan 3 讓tb...