Python_Example_商品結算程式

來源:互聯網
上載者:User

標籤:word   code   購物   proc   pytho   style   product   input   char   

2018-09-09 

IDE: Pycharm2018.02   Python 3.7  

KeyWord :   List  python 基本語句

Explain:


購物車程式

1運行程式,輸入預付款,

2允許user根據商品編號購買商品

3user選擇商品後,檢查餘額是否足夠,足夠直接付款,不夠提醒user

4可隨時退出,退出時,列印已經購買的商品和餘額‘‘‘

 

1------------------------------------------------------------------------------------------------------------------

--

code:

--

import sys‘‘‘__author__ = "chu ge "購物車程式1運行程式,輸入預付款,2允許user根據商品編號購買商品3user選擇商品後,檢查餘額是否足夠,足夠直接付款,不夠提醒user4可隨時退出,退出時,列印已經購買的商品和餘額‘‘‘product_list =[(‘Book‘,17.99),               (‘Phone‘,4999),               (‘Cothes‘,298),               (‘Apple‘,32.76),               (‘Break‘,98),               (‘Milk‘,55),               (‘Grape‘,25),               (‘Wine‘,198),               (‘Tea‘,76.89),               (‘Biscits‘,49)               ]# 提示print(‘------------------------------------------------‘)print(‘Input Your Payment ‘)payment =input(">>> ")print(‘Payment: %s 元‘ %(payment))# 加入購物車的空列表shopping_list = []if not payment.isdigit():  # 判斷是否是數字,如果不是直接退出    print(‘請輸入正取的金額...‘)    exit()else:    # 如果是數字,繼續執行    payment = int(payment)    while True:            # for item in product_list:            #     print(product_list.index(item),item)  # product_list.index(item) 擷取下標            # 列印商品資訊        for index,item in enumerate(product_list) :    # enumerate 把列表下標取出來                    print(index,item)     # product_list.index(item) 擷取下標        user_choice = input("請選擇要購買的商品?\n>>>:")            # 判斷輸入是否為數字        if user_choice.isdigit():            user_choice = int(user_choice)                #判斷輸入長度(範圍)            if user_choice< len(product_list) and  0 <= user_choice:                p_item = product_list[user_choice]                if p_item[1] <= payment: # 足夠付款 添加購物車                    shopping_list.append(p_item)                    payment -= p_item[1]                    print("商品資訊:\033[32;1m %s\033[0m \t 付款餘額: \033[31;1m %s\033[0m 元" %(p_item,payment))                else:                    print("足額不足: \033[35;1m %s\033[0m  元" %(payment))            else:                print(‘Product Not List ...‘)        elif user_choice == ‘q‘:  # quit            print(" Shopping List....")            for q in shopping_list:                print(q)            print(‘Your Payment: %s ‘%payment)            exit()        # break

--

Run Result :

--

------------------------------------------------Input Your Payment >>> 10000Payment: 10000 元0 (‘Book‘, 17.99)1 (‘Phone‘, 4999)2 (‘Cothes‘, 298)3 (‘Apple‘, 32.76)4 (‘Break‘, 98)5 (‘Milk‘, 55)6 (‘Grape‘, 25)7 (‘Wine‘, 198)8 (‘Tea‘, 76.89)9 (‘Biscits‘, 49)請選擇要購買的商品?>>>:1商品資訊: (‘Phone‘, 4999)  付款餘額:  5001 元0 (‘Book‘, 17.99)1 (‘Phone‘, 4999)2 (‘Cothes‘, 298)3 (‘Apple‘, 32.76)4 (‘Break‘, 98)5 (‘Milk‘, 55)6 (‘Grape‘, 25)7 (‘Wine‘, 198)8 (‘Tea‘, 76.89)9 (‘Biscits‘, 49)請選擇要購買的商品?>>>:q Shopping List....(‘Phone‘, 4999)Your Payment: 5001 Process finished with exit code 0

  --

 

Python_Example_商品結算程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.