标签:div 分享图片 col 图片 name code pre alt com
enumerate 作用:新增一列,自动累加(类似表格中的自增列),一般用于for循环中
例1:
dic = ["电脑","鼠标","内存","键盘"] for i,j in enumerate(dic,0): print(i,j) inp = input("请输入0-3:") inp2 = int(inp) res = dic[inp2] print("你选择的商品是: " + res)
运行效果:
例2:
dic = [{"name":"电脑","price":4500},{"name":"鼠标","price":35},{"name":"内存","price":200},{"name":"键盘","price":30}] for i,j in enumerate(dic,0): print(i,j["name"],j["price"]) inp = input("请输入0-3:") inp2 = int(inp) res = dic[inp2]["name"] res1 = dic[inp2]["price"] print("你选择的商品是: " + res + " 价格为:" +str(res1))
标签:div 分享图片 col 图片 name code pre alt com
原文地址:https://www.cnblogs.com/junzhu2/p/9059962.html