products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["coffee", 31], ["book", 60], ["Nike", 6991]]
i = 0
print("-" * 5 + " " + "商品列表" + " " + "-" * 5)
for product in products:
print("%d" % i, end="\t")
for t in product:
print("%s" % t, end="\t")
i += 1
print("\t")
market = []
while 1:
t = input("您想要买什么呢?")
if t != "q":
market.append(products[int(t)][0])
elif t == "q":
print(market)
break
学习了enumerate函数之后(enumerate可以同时输出数据和数据下标,一般应用在for循环中
# 使用列举函数 enumerate
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["coffee", 31], ["book", 60], ["Nike", 6991]]
print("-" * 5 + " " + "商品列表" + " " + "-" * 5)
for i, product in enumerate(products):
print("%d" % i, end="\t")
for t in product:
print("%s" % t, end="\t")
print("\t")