zoukankan      html  css  js  c++  java
  • 跟着Alex老师学习抄了一遍shopping_list的购物程序

    这段小程序基本上涵盖了,老师最近讲解的while循环,if,else,逻辑判断。还有重要的一点就是列表。并且在列表里面取下标。

    取下标的方法,老师讲了两种。1、print (product_list.index(item),item)   #这样可以显示下标,最基本的方法

                  2、利用enumerate来枚举列表中下标,具体表示方法如下。for index, item in enumerate (product_list):

                                                                                                              print (index,item)

    利用取下标的方法,可以很容易取到列表里产品的序号。

    另外就是字体去颜色,这个是需要死记的。

    33[32;1m %s 33[0m
    具体参照上面这个。
    附代码。
    __author__ = "david.z"
    
    product_list = [
        ('苹果手机',5800),
        ('苹果电脑',9800),
        ('自行车',800),
        ('高档手表',10600),
        ('辛巴克咖啡',31),
        ('Python书籍',120),
    ]
    shopping_list = []
    salary = input("输入你的工资:")
    while True:
    
        if salary.isdigit():
            salary = int(salary)
            while True:
                for index, item in enumerate(product_list):
                    #print(product_list.index(item),item)
                    print (index,item)
                user_choice = input("请选择您要购买的商品?>>>:")
                if user_choice.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: #买得起
                            shopping_list.append(p_item)
                            salary -= p_item[1]
                            print("你所购买的商品为 33[32;1m%s33[0m,你的余额还剩 33[31;1m%s33[0m"%(p_item,salary))
                        else:
                            print ("33[41;1m您的余额仅剩%s,请尽快充值!!33[0m"%salary)
                    else:
                        print("您选择的[%s]不存在!!!"%user_choice)
                elif user_choice == 'q'or user_choice == 'Q':
                    print("---------shopping_list-----------")
                    for p in shopping_list:
                        print(p)
                    print("您的余额为:",salary)
                    exit()
                else:
                    print("您的输入有误")
        else:
            print("您的输入有误,请重新输入!!")
        break
  • 相关阅读:
    poj3195 Generalized Matrioshkas(瞎搞题翻译)
    hdu2544最短路
    hdu2544最短路
    poj3195 Generalized Matrioshkas(栈)
    poj3195 Generalized Matrioshkas(栈)
    bzoj3171 [Tjoi2013]循环格
    bzoj3171 [Tjoi2013]循环格
    bzoj2245 [SDOI2011]工作安排
    bzoj2245 [SDOI2011]工作安排
    bzoj2668 [cqoi2012]交换棋子
  • 原文地址:https://www.cnblogs.com/davidz/p/8306058.html
Copyright © 2011-2022 走看看