首页 > 代码库 > Day6

Day6

 #Author:leon


product_list = [
    (huawei,5000),
    (xiaomi,4000),
    (oppo,3000),
    (leshi,2000),
 ]
shopping_list = [] #一个空的列表
salary = input(input your salary:) #输入你的工资
if salary.isdigit():                  #输入的字符为数字
    salary = int(salary)              #整形,变成整数
    while True:                      #当以下条件成立
       for index,i in enumerate(product_list): #index 下角标,enumerate把下脚标取出来
            #print(product_list.index(i),i)
            print(index,i)
       user_choice = input(what do you want)
       if user_choice.isdigit():            #.isdigit所有字符都是字数
          user_choice = int(user_choice)    #整形
          if user_choice<len(product_list) and user_choice>=0:
              p_item = product_list[user_choice]
              if p_item[1]<=salary:          #product_list列表中 商品=0,价格=1
                  shopping_list.append(p_item)#在这个购物车列表中添加已购买的商品
                  salary -= p_item[1]       #减去商品的价格                   #/033[31;m   [0m 涂上颜色
                  print(add %s into shopping cart,your current balance is \033[31;1m%s\033[0m%(p_item,salary))
              else:
               print(\033[41;1m你的余额只剩%s\033[0m%salary)
          else:
               print(product [%s] is not exist%  user_choice)
       elif user_choice == q:     #退出
           print(---------shopping list---------)
           for p in shopping_list:
               print(p)
           print(your current balance:,salary)
           exit()
       else:
           print(invaild option)

 

Day6