首页 > 编程语言 > 详细

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

时间:2017-05-31 23:42:23      阅读:618      评论:0      收藏:0      [点我收藏+]

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

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!