元组-琢磨已久的购物车程序

#!\usr\bin\env\python

# _*_coding:utf -8  _*9

product_list =[                                                     #定义一个列表,使用列表嵌套方法

       ('Iphone',5800),

       ('Mac Pro',9800),

       ('Bike',800),

       ('Watch',10600),

       ('Coffee',31),

       ('Alex',120),

]

shopping_list = []

salary = input("Input your salary:  ")             #输入工资

if salary.isdigit():                                  #isdigit方法是用来判断工资的数字是不是整数,是整数就转换为int类型

     salary = int(salary)

     while True:

           for index,item in enumerate(product_list):           #enumerate方法是用来取下标,

                print(index,item)                                   #打印商品列表

           user_choice = input("选择要买吗>>:")

          if user_choice.isdigit():                               #判断用户选择的数字是不是整数,是则转换为Int类型

              user_choice = int(user_choice)

              if user choice <len(product_list) and user_choice >=0:         #len方法是避免写死条件,例如:f>0,f<6,使用len方法后写法如左语句

                 p_item[1] = product_list[user_choice]              #通过下标把商品取出来

                 if p_item[1]<=salary:                                      #判定工资是否买的起商品

                    shopping_list.append(p_item)                 #买得起则添加购物车

                    salary - = p_item[1]                                    #买完后进行扣钱

                    print("Added %s into shopping cart ,your curent balance is \033[31;1m%s\033[0m " %(p_item,salary))                  #买的起打印输出

                 else:                                                                #如果买不起打印输出

                     print("\033[41m;1m你的余额只剩[%s]啦,还买什么东西\033[0m" % salary)

            else:                                            #输入商品序号不存在

               print("product code [%s] is not exist!")

        elif  user_choice == 'q':

            print("-----shopping list------" %s user_choice)                                  #打印已经输出的商品列表

            for p in shopping_list:                                     #对购买产品后的商品进行判断退出

                  print(p)

            print("your current balance:",salary)              #剩余的工资余额

            exit()                          #退出方法

   else:

      print("invalid option")

 

转载于:https://www.cnblogs.com/lindong0602/p/8821332.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值