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


    #!usrinenvpython

    # _*_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 33[31;1m%s33[0m " %(p_item,salary))                  #买的起打印输出

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

                         print("33[41m;1m你的余额只剩[%s]啦,还买什么东西33[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")

  • 相关阅读:
    普通父类和抽象父类
    properties文件的读取和写入
    HTTPS接口获取数据报错的问题
    SpringMVC中bean之间的相互应用(循环引用)
    JAVA代码进行排序以及模糊查询
    从TXT读取文本和将数据写入TXT文本的两种方式
    EXCEL最简洁导出(不带样式设置)
    5种线程池的创建方法和作用
    html5是什么
    J2EE,JSP,Java的区别
  • 原文地址:https://www.cnblogs.com/lindong0602/p/8821332.html
Copyright © 2020-2023  润新知