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

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,测试

1

23

posted @ 2017-05-31 22:42  包八力  阅读(130)  评论(0编辑  收藏  举报