python路5__購物車小程式練習

來源:互聯網
上載者:User

標籤:blog   沒有   break   bin   class   one   ota   please   append   

1,購物車小程式

需求:

(1),啟動程式後,列印全部商品列表,使用者輸入工資
(2),許使用者根據商品編號購買商品
(3),使用者購買商品,餘額不足就退出程式,並列印資訊
(4),可隨時退出,退出時,列印已購買商品和餘額

2,流程圖

3,代碼

#!/usr/bin/python3Product_list = [    (‘Doido鑽戒 ‘,8000),    (‘ROLEX手錶‘,20000),    (‘HuaWei P10‘,4000),    (‘AppleWatch‘,2000),    (‘Ipad‘,1000),    (‘Asics‘,500)]Shopping_list = []for i in enumerate(Product_list):    print(i)TotalSalary = input(‘Your salary oror (q to quit process):‘)while True:    AvailableProduct = []    if TotalSalary == ‘q‘:        print(‘什麼也沒有買!!‘)        break    for  i in range(len(Product_list)):        if int(TotalSalary) >= Product_list[i][1]:            AvailableProduct.append(Product_list[i])    if  len(AvailableProduct):        for i in enumerate(AvailableProduct):            print(i)        UserChoice = input("Please enter product‘s code that you want to buy(or q to quit):")        if   not UserChoice.isdigit():            print(‘慢走,歡迎下次光臨!!‘)            print (‘已購買:%s,餘額:%s‘ % (Shopping_list,TotalSalary))            break        Shopping_list.append(AvailableProduct[int(UserChoice)][0])        TotalSalary = int(TotalSalary) - AvailableProduct[int(UserChoice)][1]    else :        print(‘Insufficient Balance‘)        print(‘已購買:%s,餘額:%s‘ % (Shopping_list, TotalSalary))        break

4,測試

python路5__購物車小程式練習

聯繫我們

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