午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:115日期:2022-06-20 15:46:09
購(gòu)物車程序需求: 用戶輸入購(gòu)物預(yù)算 展示商品列表 用戶購(gòu)買商品,每次購(gòu)買后提示用戶購(gòu)買信息和剩余預(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 Trueelse: 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(’%dt%-10st%s’ % (index + 1, __object[index][0], __object[index][1])) except Exception:return None# 定義初始商品列表和購(gòu)物車列表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)買商品編號(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) - 1product_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)買成功,購(gòu)買了%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)買成功,您的預(yù)算已花完.’) breakelse: print(’余額不足,請(qǐng)重新’, end=’’) else:print(’輸入有誤,請(qǐng)重新’, end=’’)# 購(gòu)物車不為空時(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)

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

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

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

輸入的預(yù)算是否小于商品最低單價(jià)校驗(yàn) 退出選項(xiàng)2 購(gòu)物2.1 直接退出

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

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

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

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

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

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

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

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

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 污污视频在线观看免费 | 国产一二区在线观看 | 久久精品天堂 | 亚洲男人精品 | 最新色网址 | 2019国产在线 | 97福利影院| 亚洲在线免费观看视频 | 在线视频麻豆 | 91亚洲精品国偷拍 | 欧美激情在线播放 | 青青草原亚洲 | 福利资源在线观看 | 亚洲草逼| 国产激情在线播放 | 久久成人黄色 | 黄色一区二区三区 | 夜夜爽天天爽 | 天天操天天干天天爽 | 国产日韩在线观看一区 | 日韩成人高清视频 | 久久超碰精品 | 偷拍亚洲综合 | 97黄色| 久草网在线观看 | 中文一二三区 | 久久久久久久久97 | 亚洲激情综合 | 国产一区一区三区 | 宅男午夜影院 | 99久久影院| 青青青草视频在线 | 91社在线| 天天久久 | 中文字幕高清在线 | 日韩久久中文字幕 | 日韩精品一区在线观看 | 糖心在线视频 | 国产精品九九 | 午夜av一区二区 | 四虎影院入口 |