python-購物車程式

來源:互聯網
上載者:User

標籤:open   輸入   產品   餘額   其他   ide   src   購物車   post   

程式練習 

程式:購物車程式

需求:

  1. 啟動程式後,讓使用者輸入工資,然後列印商品列表
  2. 允許使用者根據商品編號購買商品
  3. 使用者選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒 
  4. 可隨時退出,退出時,列印已購買商品和餘額 
     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-購物車程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.