購物車登陸 購物版

2022-05-29 08:30:21 字數 3958 閱讀 5537

*****************購物車

import os

product_list = [['iphone7',5800],

['coffee',30],

['疙瘩湯',10],

['python book',99],

['bike',199],

['vivo x9',2499],

]shopping_cart={}

current_userinfo=

db_file=r'db.txt'

while true:

print('''

1 註冊

2 登入

3 購物

''')

choice=input('請選擇:').strip()

if choice =='1':

while true:

name=input('username:')

password=input('password:')

conf_password=input('conf password')

balance=input('balance:')

if password ==conf_password:

with open(db_file,'a') as f:

f.write('%s,%s,%s\n'%(name,password,balance))

break

else:

print('兩次密碼不一致')

elif choice =='2':

count=0

tag=true

while tag:

if count ==3:

print('錯誤過多,退出')

break

name=input('name')

password=input('password')

with open(db_file,'r') as f:

for line in f:

user_info=line.split(',')

user_name=user_info[0]

user_password=user_info[1]

user_balance=int(user_info[2])

if user_name == name and user_password == password:

current_userinfo=[user_name,user_balance]

print('登入成功')

print('使用者資訊為:',current_userinfo)

tag=false

break

else:

print('使用者名稱密碼錯誤')

count+=1

elif choice == '3':

if len(current_userinfo) == 0:

print('\033[49m請先登陸...\033[0m')

else:

#登陸成功後,開始購物

uname_of_db=current_userinfo[0]

balance_of_db=current_userinfo[1]

print('尊敬的使用者[%s] 您的餘額為[%s],祝您購物愉快' %(

uname_of_db,

balance_of_db

))tag=true

while tag:

for index,product in enumerate(product_list):

print(index,product)

choice=input('輸入商品編號購物,輸入q退出》: ').strip()

if choice.isdigit():

choice=int(choice)

if choice < 0 or choice >= len(product_list):continue

pname=product_list[choice][0]

pprice=product_list[choice][1]

if balance_of_db > pprice:

if pname in shopping_cart: # 原來已經購買過

shopping_cart[pname]['count']+=1

else:

shopping_cart[pname]=

balance_of_db-=pprice # 扣錢

current_userinfo[1]=balance_of_db # 更新使用者餘額

print(pname + " 新增到購物車,餘額為: " + str(balance_of_db))

else:

print("產品**是,你還差".format(

price=pprice,

lack_price=(pprice - balance_of_db)

))print(shopping_cart)

elif choice == 'q':

print("""

---------------------------------已購買商品列表---------------------------------

id 商品 數量 單價 總價

""")

total_cost=0

for i,key in enumerate(shopping_cart):

print('%22s%18s%18s%18s%18s' %(

i,key,

shopping_cart[key]['count'],

shopping_cart[key]['pprice'],

shopping_cart[key]['pprice'] * shopping_cart[key]['count']

))total_cost+=shopping_cart[key]['pprice'] * shopping_cart[key]['count']

print("""

您的總花費為: %s

您的餘額為: %s

---------------------------------end---------------------------------

""" %(total_cost,balance_of_db))

while tag:

inp=input('確認購買(yes/no?)>>: ').strip()

if inp not in ['y','n','y','n','yes','no']:continue

if inp in ['y','y','yes']:

# 將餘額寫入檔案

src_file=db_file

dst_file=r'%s.swap' %db_file

with open(src_file,'r',encoding='utf-8') as read_f,\

open(dst_file,'w',encoding='utf-8') as write_f:

for line in read_f:

if line.startswith(uname_of_db):

l=line.strip('\n').split(',')

l[-1]=str(balance_of_db)

line=','.join(l)+'\n'

write_f.write(line)

os.remove(src_file)

os.rename(dst_file,src_file)

print('購買成功,請耐心等待發貨')

shopping_cart={}

current_userinfo=

tag=false

else:

print('非法輸入')

購物車0612版登陸與購物功能

使用者名稱和密碼存放於檔案中,格式為 egon egon123 啟動程式後,先登入,登入成功則讓使用者輸入工資,然後列印商品列表,失敗則重新登入,超過三次則退出程式 允許使用者根據商品編號購買商品 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 可隨時退出,退出時,列印已購買商品和餘額 ...

購物車(註冊 登入 購物 購物車 結帳)

購物車 註冊 登入 購物 購物車 結帳 shopping car dict dict money 0 def input username pwd username input username pwd input pwd return username,pwd def goods get with...

Servlet版購物車

1.通過servlet來構造乙個最簡單的購物車,servlet是jsp的基礎,因此利用這個例子來看看servlet的一些要點。因此,這裡做一下限制,只能使用servlet和html。html 如下 pears go to checkout 要點 這部分用於控制禁止客戶端快取。2.selections...