第十天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 是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数

可以用来文件签名,md5加密,和密码验证,不同的消息可能得到相同的hash值,因此使用hash过程中,要防止hash冲突,

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

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

原文地址:https://www.cnblogs.com/xudachen/p/8315811.html