使用python的列表实现购物车程序

# Author Richard_Kong
# !/usr/bin/env python
# --*-- encoding:utf-8 --*--
"""
请闭眼写出以下程序。
程序:购物车程序

需求:

    启动程序后,让用户输入工资,然后打印商品列表
    允许用户根据商品编号购买商品
    用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    可随时退出,退出时,打印已购买商品和余额
"""
shopping_cart = []
shopping_goods = [["book",20],["cup",30],["pen",38],["laptop",4000],["Phone",3800],
                  ["watch",2800]]
salary = int(input("Please input your salary:"))
while True:
    for i in shopping_goods:
        print(shopping_goods.index(i),",",i[0],":",i[1])
    print("Please select your goods")
    command = input("Please input the command:")

    for i in shopping_goods:
        if command.isdigit():
            if int(command) == shopping_goods.index(i):
                print("*******************")
                if salary-i[1] >=0:
                    shopping_cart.append(i)
                    salary = salary - i[1]
                    print("your balance is :",salary)
                else:
                    print("your balacne is low please choose another good")

    if command == "exit":
        print("your shopping cart is :",shopping_cart)
        print("your balance is :",salary)
        break

猜你喜欢

转载自blog.csdn.net/kokodudu/article/details/81369071