python入门牛刀小试04

"""
1、超市物品信息
把商品信息存储在列表中,通过输入商品名称,控制台显示商品信息
"""
#列表法
goods = [["牛肉干",118],["酱牛肉",50],["卫龙辣条",3],["唐僧肉",0.5],["牛肉面",4.5]]
# 提示用户输入查询内容
good = input("请输入商品名称:")
#外层循环,outside表示一维列表的下标
outside = 0
while outside < len(goods):
    inner = 0
    while inner < len(goods[outside]):
        if goods[outside][inner] == good:
            print("%s的价格是:%f"%(good,goods[outside][inner+1]))
            # #请输入新的价格
            # price = float(input("请输入新的价格:"))
        inner += 1
    outside += 1


# 元组法
goods = (("牛肉干",118),("酱牛肉",50),("卫龙辣条",3),("唐僧肉",0.5),("牛肉面",4.5))
good = input("请输入商品名称:")
out = 0
outside = 0
while outside < len(goods):
    inner = 0
    while inner < len(goods[outside]):
        if goods[outside][inner] == good:
            print("%s的价格是:%f"%(good,goods[outside][inner+1]))
            # #请输入新的价格
            # price = float(input("请输入新的价格:"))
        inner += 1
    outside += 1


"""
2、实现功能:
列表为:['Iphone8',6888],['MacPro',14800],['小米6',2499],['Coffee',31],['Book',80],['Nike Shoes',799]
实现一个类似购物车的语句,用户循环输入编号可以将列表内的商品储存到一个新的列表里面,当用户输入'q'时退出循环
"""
products = [['Iphone8', 6888], ['MacPro', 14800], ['小米6', 2499], ['Coffee', 31], ['Book', 80], ['Nike Shoes', 799]]
shopping_car = []
while True:
    print("-------商品信息---------")
    for index,i in enumerate(products):
        print("%s.%s  %s"%(index,i[0],i[1]))
    chose = input("请输入你想添加购物车的商品信息(q键退出):")
    if chose.isdigit():
        chose = int(chose)
        shopping_car.append(products[chose])
    elif chose == "q":
        print("---------购物车商品信息-----------")
        for index,i in enumerate(shopping_car):
            print("%s.%s  %s"%(index,i[0],i[1]))
        break

猜你喜欢

转载自blog.csdn.net/qq_42336700/article/details/81410667