爬虫 复习python基础知识(购物车)

products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
print("-"*6+"\t"+"商品列表"+"\t"+"-"*6)
for i in range(6):
    print(i,end=" ")
    for j in range(2):
        #suojin=
        print(products[i][j],end="")
        if (i != 2)and(j == 0):
            print(" "*(2+6-len(products[i][j])),end="")
        elif (i == 2)and(j == 0):
            print(" "*(2+6-len(products[i][j])-1),end="")
        #中文和英文占位不同小米6单独考虑
    print("\n")
shopping_cart=[]
while True:
    yong_hu=input("请输入你想购买的商品编号:(按q退出)")
    if yong_hu in ["0","1","2","3","4","5","q"]:
        if yong_hu == "q":
            for i in range(len(shopping_cart)):
                print(shopping_cart[i], end="\t")
            break
        shopping_cart.append(products[int(yong_hu)][0])
    else:
        print("输入格式错误,请输入0~5或者q")

结果
爬虫 复习python基础知识(购物车)_第1张图片

你可能感兴趣的:(爬虫,练习,列表,python)