標籤:open 輸入 產品 餘額 其他 ide src 購物車 post
程式練習
程式:購物車程式
需求:
- 啟動程式後,讓使用者輸入工資,然後列印商品列表
- 允許使用者根據商品編號購買商品
- 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒
- 可隨時退出,退出時,列印已購買商品和餘額
1 # -*- coding:utf-8 -*- 2 # !/usr/bin/env python 3 # Author:dc 4 5 list_of_product = [["IphoneX", 8999], ["Apple Air", 3088], ["MacBook Air", 5999], ["MacBookPro", 128000], 6 ["Iphone6", 4999]] 7 list_of_shopping = [] 8 money_of_guest = input("請輸入您的預存款:") 9 if money_of_guest.isdigit():10 money_of_guest = int(money_of_guest)11 while True:12 13 print("\n商店產品列表如下\n")14 15 for l in list_of_product:16 print(list_of_product.index(l), l)17 18 num_product = input("請輸入您需要購買商品的序號:")19 20 if num_product.isdigit():21 num_product = int(num_product)22 23 if money_of_guest < list_of_product[num_product][1]:24 print("您的餘額不足以購買此商品,請選擇其他商品或輸入q退出")25 else:26 print("已將{name_of_product}加入您的購物車".format(name_of_product=list_of_product[num_product][0]))27 money_of_guest = money_of_guest - list_of_product[num_product][1]28 print("您的餘額還有{b}".format(b=money_of_guest), "元")29 list_of_shopping.append(list_of_product[num_product])30 elif num_product == "q":31 print("\n您的購物清單如下:\n")32 for i in list_of_shopping:33 print(i)34 print("您的餘額還有{b}".format(b=money_of_guest), "元,歡迎下次再來!")35 exit()36 else:37 print("請輸入正確的金額")View Code
python-購物車程式