Python如何實(shí)現(xiàn)的簡單購物車程序
購物車程序需求:
- 用戶輸入購物預(yù)算
- 展示商品列表
- 用戶購買商品,每次購買后提示用戶購買信息和剩余預(yù)算
- 購物完成后打印購物花費(fèi)和購物清單,并將商品從原列表移除
實(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 # 定義初始商品列表和購物車列表 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!') # 輸入購物預(yù)算,并校核預(yù)算是否合法 while True: budget_input = input('您的購物預(yù)算是多少:') if is_positive_int(budget_input): budget = int(budget_input) break else: print('輸入有誤,請重新輸入.', end='') # 首次打印商品列表 print('Product list:') print_list(product_list) # 進(jìn)入購物程序 while len(product_list) > 0: choice = input('選擇購買商品編號[退出:quit]:') if choice == 'quit': break # 校驗(yàn)輸入的商品編號是否存在 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] # 余額判斷購物是否成功 if budget > product_price: budget = budget - product_price product = product_list.pop(product_index) product_list_shopped.append(product) print('購買成功,購買了%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('購買成功,您的預(yù)算已花完.') break else: print('余額不足,請重新', end='') else: print('輸入有誤,請重新', end='') # 購物車不為空時(shí),打印購物列表和花費(fèi) if product_list_shopped: sum_price = sum(x[1] for x in product_list_shopped) print('您一共花費(fèi)%d,購物清單如下:' % sum_price) print_list(product_list_shopped) print('歡迎下次光臨!')
代碼測試如下
1 預(yù)算校驗(yàn)
預(yù)算輸入限制為正整數(shù),其余輸入均會(huì)提示并要求重新輸入
預(yù)算校驗(yàn)可新增:
- 輸入的預(yù)算是否小于商品最低單價(jià)校驗(yàn)
- 退出選項(xiàng)
2 購物
2.1 直接退出
2.2 單次購物花完預(yù)算
2.3 多次購物花完預(yù)算
2.4 多次購物后主動(dòng)退出
2.5 商品被購買完
以上就是Python如何實(shí)現(xiàn)的簡單購物車程序的詳細(xì)內(nèi)容,更多關(guān)于python 購物車程序的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Tensorflow 利用tf.contrib.learn建立輸入函數(shù)的方法
這篇文章主要介紹了Tensorflow 利用tf.contrib.learn建立輸入函數(shù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Python網(wǎng)絡(luò)爬蟲實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Python網(wǎng)絡(luò)爬蟲實(shí)例,爬蟲的定義、主要框架等基礎(chǔ)概念,感興趣的小伙伴們可以參考一下2016-04-04Python OpenCV 圖像矯正的原理實(shí)現(xiàn)
這篇文章主要介紹了Python OpenCV 圖像矯正的原理實(shí)現(xiàn),檢測邊緣點(diǎn);以邊緣點(diǎn)作為輸入,采用Hough直線檢測,檢測出最多點(diǎn)共線的四條直線,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07詳解Python如何檢查一個(gè)數(shù)字是否是三態(tài)數(shù)
在數(shù)學(xué)中,三態(tài)數(shù)(Triangular?Number)是一種特殊的數(shù)列,它是由自然數(shù)按照一定規(guī)律排列而成的,本文主要介紹了如何使用Python檢查判斷一個(gè)數(shù)字是否是三態(tài)數(shù),需要的可以參考下2024-03-03matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖
這篇文章主要介紹了matplotlib bar()實(shí)現(xiàn)百分比堆積柱狀圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02修復(fù)Python?Pandas數(shù)據(jù)標(biāo)記錯(cuò)誤的幾種方法總結(jié)
用于分析數(shù)據(jù)的?Python?庫稱為?Pandas,在?Pandas?中讀取數(shù)據(jù)最常見的方式是通過?CSV?文件,但?CSV?文件的限制是它應(yīng)該采用特定的格式,否則在標(biāo)記數(shù)據(jù)時(shí)會(huì)拋出錯(cuò)誤,在本文中,我們將討論修復(fù)?Python?Pandas?錯(cuò)誤標(biāo)記數(shù)據(jù)的各種方法2023-10-10python ansible自動(dòng)化運(yùn)維工具執(zhí)行流程
ansible是基于 paramiko 開發(fā)的,并且基于模塊化工作,本身沒有批量部署的能力,接下來通過本文給大家分享python ansible自動(dòng)化運(yùn)維工具的特點(diǎn)及執(zhí)行流程,感興趣的朋友跟隨小編一起看看吧2021-06-06