溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python怎么實(shí)現(xiàn)購物車

發(fā)布時(shí)間:2021-11-25 09:23:35 來源:億速云 閱讀:135 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)python怎么實(shí)現(xiàn)購物車的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

#!/usr/bin/env python
#!-- coding:utf-8 --
#Author:Eric

My_salary = input("Input your salary:")

if My_salary.isdigit():
My_salary = int(My_salary)
else:
exit("Invaild data type...")

welcome_msg = 'Welcome to Eric Shopping mall'.center(50,'-')
print(welcome_msg)

exit_flag = False
product_list = [
('Iphone',5800),
("Mac Air",8000),
("Mac Pro",9000),
("XiaoMi 2",19.9),
("Coffee",30),
("Tesla",820000),
("Bike",700),
("Cloth",200),]
shop_car = []
while exit_flag is not True:
#for product_item in product_list:
#p_name,p_price = product_item
print("product list".center(50,'-'))
for item in  enumerate(product_list):  #將列表中的下標(biāo)進(jìn)行返回。枚舉函數(shù)
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print (index,'.',p_name,p_price)
#print(len(product_list))
user_choice = input("[q = quit,c=check] What do you want to Buy?:")

if user_choice.isdigit():  #肯定是要選擇商品。
    user_choice = int(user_choice)
    if user_choice < len(product_list):
        p_item = product_list[user_choice]
        if p_item[1] <= My_salary:
            shop_car.append(p_item)  #放入購物車
            My_salary -=p_item[1]   #減錢
            print("Added [%s] into shop car,your current balance is \033[31;1m[%s]\033[0m"
                  %(p_item,My_salary))
        else:
            print("Your balance is [%s],cannot afford this..." %[My_salary])
else:
    if user_choice == 'q' or user_choice == 'quit':
        print("purchased products as below".center(40,'*'))
        for item in shop_car:
            print(item)
        print("END".center(40,'*'))
        print('Your balance is [%s]' %My_salary)
        exit_flag = True
    if user_choice == 'c' or user_choice == 'check':
        print("purchased products as below".center(40,'*'))
        for item in shop_car:
            print(item)
        print("END".center(40,'*'))
        print('Your balance is [%s]' %My_salary)

感謝各位的閱讀!關(guān)于“python怎么實(shí)現(xiàn)購物車”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI