码迷,mamicode.com
首页 > 其他好文 > 详细

2stDay

时间:2017-09-20 20:55:05      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:while   mac pro   att   product   car   and   ***   choice   sed   

技术分享
product_list =[(Iphonex,8300),
               (Moto,6900),
               (Mac Pro,18900),
               (Latte,30),
               (Mix,3999)]

yourSalary = input("Please print your Salary:")

shopping_list = []     #添加购物车
if yourSalary.isdigit():
    yourSalary = int(yourSalary)
    while True:
        ‘‘‘
         1.列出商品编号,还有商品
        for item in product_list:
            print(product_list.index(item)+1,item)
        ‘‘‘
        for index,item in enumerate(product_list):     #枚举方式
            print(index+1,item)
        your_choice = input("选择要买的商品>>>:")
        if your_choice.isdigit():
            your_choice = int(your_choice)-1
            if your_choice < len(product_list) and your_choice >= 0:#判断选择是否在范围内
                p_item = product_list[your_choice]    #选出选择的商品
                if p_item[1]<= yourSalary :           #买得起
                    shopping_list.append(p_item)#添加到购物车
                    yourSalary -=p_item[1]     #从卡里扣钱
                    print("Added %s into shopping car,your balance is "
                          "\033[31;1m%s\033[0m" %(p_item,yourSalary)) #  31--设置字体颜色
                else:
                    print("\033[41;1mYour last money just %s,go working!\033[0m"
                          %(yourSalary))#  %s相当于占位符,给%用   41--设置字体背景颜色
                # \033[41;1m************************\033[0m
            else:
                print ("Wrong written!")
        elif your_choice == q:
            print("------------shopping list-------------")
            for p in shopping_list:
                print(p)
            print("your Salary:",yourSalary)
            exit()
        else:
            print ("invalid")

else:
    print("Wrong Writen!")
购物车练习

 

2stDay

标签:while   mac pro   att   product   car   and   ***   choice   sed   

原文地址:http://www.cnblogs.com/PurpleSun/p/7563435.html

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