第十天Python学习记录

"""写一个循环,不断的问用户想买什么,用户选择一个商品编号,就把对应的商品添加到购物车里,
最终用户输入q退出时,打印购物车里的商品列表"""


products = [['iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]
shopping_cart = []
run_flag = True  # 标志位
while run_flag:  # 不断循环提示用户
    print("-----商品列表------")
    for index, p in enumerate(products):
        print("%s. %s %s" % (index, p[0], p[1]))

    choice = input("输入想买的商品编号: ")
    if choice.isdigit():  # 判断用户输入
        choice = int(choice)
        if 0 <= choice < len(products):
            shopping_cart.append(products[choice])
            print("Added product %s into shopping cart." % (products[choice]))
        else:
            print("商品不存在")
    elif choice == 'q':
        print("-----你已购买如下商品-----")
        for index, p in enumerate(shopping_cart):
            print("%s. %s %s" % (index, p[0], p[1]))
        # break
        run_flag = False

hash 是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数git

能够用来文件签名,md5加密,和密码验证,不一样的消息可能获得相同的hash值,所以使用hash过程当中,要防止hash冲突,数据库

只有不可变的类型才支持hash,例如数字,字符串,和元组,可是可变类型的列表则不支持hash,app

元组能够理解为只读的列表,不可变,功能有 索引,count和切片 一般使用在显示的告知别人,此处数据不可修改,以及数据库链接配置信息等iphone

相关文章
相关标签/搜索