国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python如何實現的簡單購物車程序

瀏覽:77日期:2022-06-20 15:46:09
購物車程序需求: 用戶輸入購物預算 展示商品列表 用戶購買商品,每次購買后提示用戶購買信息和剩余預算 購物完成后打印購物花費和購物清單,并將商品從原列表移除

實現代碼如下:

# 正整數校驗函數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# 打印商品列表函數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# 定義初始商品列表和購物車列表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!’)# 輸入購物預算,并校核預算是否合法while True: budget_input = input(’您的購物預算是多少:’) if is_positive_int(budget_input):budget = int(budget_input)break else:print(’輸入有誤,請重新輸入.’, end=’’)# 首次打印商品列表print(’Product list:’)print_list(product_list)# 進入購物程序while len(product_list) > 0: choice = input(’選擇購買商品編號[退出:quit]:’) if choice == ’quit’:break # 校驗輸入的商品編號是否存在 elif is_positive_int(choice) and 0 < int(choice) < len(product_list) + 1:product_index = int(choice) - 1product_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,花費%d,您的剩余預算為:%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(’購買成功,您的預算已花完.’) breakelse: print(’余額不足,請重新’, end=’’) else:print(’輸入有誤,請重新’, end=’’)# 購物車不為空時,打印購物列表和花費if product_list_shopped: sum_price = sum(x[1] for x in product_list_shopped) print(’您一共花費%d,購物清單如下:’ % sum_price) print_list(product_list_shopped)print(’歡迎下次光臨!’)代碼測試如下1 預算校驗

Python如何實現的簡單購物車程序

預算輸入限制為正整數,其余輸入均會提示并要求重新輸入

預算校驗可新增:

輸入的預算是否小于商品最低單價校驗 退出選項2 購物2.1 直接退出

Python如何實現的簡單購物車程序

2.2 單次購物花完預算

Python如何實現的簡單購物車程序

2.3 多次購物花完預算

Python如何實現的簡單購物車程序

2.4 多次購物后主動退出

Python如何實現的簡單購物車程序

2.5 商品被購買完

Python如何實現的簡單購物車程序

以上就是Python如何實現的簡單購物車程序的詳細內容,更多關于python 購物車程序的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 尤溪县| 塔城市| 温州市| 邵东县| 涪陵区| 英山县| 温州市| 微山县| 平塘县| 永顺县| 吉水县| 仪陇县| 交城县| 秀山| 永平县| 苍南县| 胶南市| 宁明县| 延吉市| 正定县| 青浦区| 五峰| 瑞昌市| 杭锦旗| 平遥县| 建平县| 营山县| 中山市| 唐山市| 红原县| 白银市| 高尔夫| 南投县| 南雄市| 东光县| 高平市| 常山县| 牟定县| 集贤县| 宿松县| 镇原县|