码迷,mamicode.com
首页 > 编程语言 > 详细

python路5__购物车小程序练习

时间:2017-06-01 00:23:12      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:blog   没有   break   bin   class   one   ota   please   append   

1,购物车小程序

需求:

(1),启动程序后,打印全部商品列表,用户输入工资
(2),许用户根据商品编号购买商品
(3),用户购买商品,余额不足就退出程序,并打印信息
(4),可随时退出,退出时,打印已购买商品和余额

2,流程图

技术分享

3,代码

#!/usr/bin/python3

Product_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__购物车小程序练习

标签:blog   没有   break   bin   class   one   ota   please   append   

原文地址:http://www.cnblogs.com/tobet/p/6926256.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!