主頁(yè) > 知識(shí)庫(kù) > Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序

Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序

熱門(mén)標(biāo)簽:悟空智電銷(xiāo)機(jī)器人6 荊州云電銷(xiāo)機(jī)器人供應(yīng)商 遼寧400電話(huà)辦理多少錢(qián) 幫人做地圖標(biāo)注收費(fèi)算詐騙嗎 江蘇房產(chǎn)電銷(xiāo)機(jī)器人廠(chǎng)家 蘇州電銷(xiāo)機(jī)器人十大排行榜 電信營(yíng)業(yè)廳400電話(huà)申請(qǐng) 溫州旅游地圖標(biāo)注 外呼不封號(hào)系統(tǒng)

購(gòu)物車(chē)程序需求:

  • 用戶(hù)輸入購(gòu)物預(yù)算
  • 展示商品列表
  • 用戶(hù)購(gòu)買(mǎi)商品,每次購(gòu)買(mǎi)后提示用戶(hù)購(gòu)買(mǎi)信息和剩余預(yù)算
  • 購(gòu)物完成后打印購(gòu)物花費(fèi)和購(gòu)物清單,并將商品從原列表移除

實(shí)現(xiàn)代碼如下:

# 正整數(shù)校驗(yàn)函數(shù)
def is_positive_int(input_num):
    # noinspection PyBroadException
    # 上一條注釋消除Pycharm 'Too broad exception clause' 警告
    try:
        positive_int = int(input_num)
        if positive_int > 0:
            return True
        else:
            return False
    except Exception:
        return False


# 打印商品列表函數(shù)
def print_list(__object):
    # noinspection PyBroadException
    # 上一條注釋消除Pycharm 'Too broad exception clause' 警告
    try:
        for index in range(0, len(__object)):
            print('%d\t%-10s\t%s' % (index + 1, __object[index][0], __object[index][1]))
    except Exception:
        return None


# 定義初始商品列表和購(gòu)物車(chē)列表
product_list = [
    ['iPhone 12', 10000],
    ['iPhone 11', 6000],
    ['HUAWEI P30', 5000],
    ['榮耀 30', 4000],
    ['小米 10', 3000],
    ['紅米 K40', 2000]
]
product_list_shopped = []

print('Welcome to shopping mall!')

# 輸入購(gòu)物預(yù)算,并校核預(yù)算是否合法
while True:
    budget_input = input('您的購(gòu)物預(yù)算是多少:')
    if is_positive_int(budget_input):
        budget = int(budget_input)
        break
    else:
        print('輸入有誤,請(qǐng)重新輸入.', end='')

# 首次打印商品列表
print('Product list:')
print_list(product_list)

# 進(jìn)入購(gòu)物程序
while len(product_list) > 0:
    choice = input('選擇購(gòu)買(mǎi)商品編號(hào)[退出:quit]:')
    if choice == 'quit':
        break
    # 校驗(yàn)輸入的商品編號(hào)是否存在
    elif is_positive_int(choice) and 0  int(choice)  len(product_list) + 1:
        product_index = int(choice) - 1
        product_price = product_list[product_index][1]
        # 余額判斷購(gòu)物是否成功
        if budget > product_price:
            budget = budget - product_price
            product = product_list.pop(product_index)
            product_list_shopped.append(product)
            print('購(gòu)買(mǎi)成功,購(gòu)買(mǎi)了%s,花費(fèi)%d,您的剩余預(yù)算為:%d' % (product[0], product_price, budget))
            print_list(product_list)
        elif budget == product_price:
            budget = budget - product_price
            product = product_list.pop(product_index)
            product_list_shopped.append(product)
            print('購(gòu)買(mǎi)成功,您的預(yù)算已花完.')
            break
        else:
            print('余額不足,請(qǐng)重新', end='')
    else:
        print('輸入有誤,請(qǐng)重新', end='')

# 購(gòu)物車(chē)不為空時(shí),打印購(gòu)物列表和花費(fèi)
if product_list_shopped:
    sum_price = sum(x[1] for x in product_list_shopped)
    print('您一共花費(fèi)%d,購(gòu)物清單如下:' % sum_price)
    print_list(product_list_shopped)
print('歡迎下次光臨!')

代碼測(cè)試如下

1 預(yù)算校驗(yàn)

預(yù)算輸入限制為正整數(shù),其余輸入均會(huì)提示并要求重新輸入

預(yù)算校驗(yàn)可新增:

  • 輸入的預(yù)算是否小于商品最低單價(jià)校驗(yàn)
  • 退出選項(xiàng)

2 購(gòu)物

2.1 直接退出

2.2 單次購(gòu)物花完預(yù)算

2.3 多次購(gòu)物花完預(yù)算

2.4 多次購(gòu)物后主動(dòng)退出

2.5 商品被購(gòu)買(mǎi)完

以上就是Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序的詳細(xì)內(nèi)容,更多關(guān)于python 購(gòu)物車(chē)程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • python實(shí)現(xiàn)簡(jiǎn)單的聊天小程序
  • 利用Python實(shí)現(xiàn)定時(shí)程序的方法
  • 基于Python+Pyqt5開(kāi)發(fā)一個(gè)應(yīng)用程序
  • 結(jié)合Python網(wǎng)絡(luò)爬蟲(chóng)做一個(gè)今日新聞小程序

標(biāo)簽:臺(tái)灣 喀什 宿遷 欽州 黃山 三沙 景德鎮(zhèn) 濟(jì)南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序》,本文關(guān)鍵詞  Python,如何,實(shí)現(xiàn),的,簡(jiǎn)單,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Python如何實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)程序的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章