python小白-----列表练习题(一个简单的购物车程序)

问题一:插入一个元素到一个列表中,开始时我是这么做的:

但却显示报错,百思不得其解啊,最后,终于发现列表中的方法都是没有返回值的,所以这一点在使用时需要注意,不然就可能会犯和我一样的错误。

问题二:一个简单的购物体验,哈哈,虽然在大佬看来很简单,但对于我这个小白还是废了一点点功夫的,这个基本上包含了列表所要用到的知识,所以就记录一下,后面有好的想法会持续改进:

products = [['iphone8', 6888], ['MacPro', 14888], ['小米6', 2499], ['Coffe', 31], ['Book', 80], ['NIke Shoes', 799]]
print("---------------商品列表--------------")
for index,i in enumerate(products):
    print("%s.%s     %s"%(index,i[0],i[1]))
shop = []
while True:
    choice = input('请选择您想要买的商品的编号:')
    if choice.isdigit():
        choice = int(choice)
        if choice>=0 and choice<len(products):
             shop.append(products[choice])
             print("您选购的商品 %s 已加入购物车"%(products[choice]))
        else:
            print("您所选购的商品不存在,请重新选择")
            continue
    else:
        print("-----------购物车------------")
        for index,p in enumerate(shop):
            print("    %s.%s  %s  "%(index,p[0],p[1]))
        break

猜你喜欢

转载自blog.csdn.net/qq_40531074/article/details/80316213