python实现控制台购物系统

#定义用户字典,用于验证登录
user = {"root": {"passwd": "123", "money": 1000},"admin": {"passwd": "admin", "money": 1000}}


#定义商品字典
product = {'f001': {"name": "apple", "price": 2.5},
                   'f002': {"name": "banana", "price": 3.5},
                   'f003': {"name": "grape", "price": 4.5},
                   'f004':{"name":"fork","price":18},
                   'f005':{"name":"sheep","price":25}}



print("-" * 50)
print('#' + "{0:^42}".format("欢迎登录三乐购物系统") + '#')
print("-" * 50)
print("温馨提示,输入quit退出系统")


#展示商品函数
def product_list():
    print("商品列表")
    for i, j in product.items():
        print(f"编号:{i},名称:{j['name']},单价:{j['price']}")

#菜单栏
def menu():
    print("菜单栏".center(40, "-"))
    print("输入1查看商品列表".center(43))
    print("输入2查看购物车 ".center(43))
    print("输入3进行购买   ".center(43))
    print("输入4结算     ".center(43))
    print("输入5退出系统   ".center(43))
    print("-" * 43)


#购物车
def cart():
    global sum_price
    print("购物车".center(40, "*"))
    print("已购买的商品:")
    for i in buy_pro:
        print(f"名称:{i[0]} 数量:{i[1]}")
    print(f"总金额为:{sum_price}")
    if len(buy_pro)==0:
        print("暂未购买物品,购物车为空")
    print("购物车".center(40, "*"))


#购买功能
def buy():
    global sum_price,have_money
    print("商品列表")
    for i, j in product.items():
        print(f"编号:{i},名称:{j['name']},单价:{j['price']}")
    print("输入quit退出购买")
    while 1:
        buy = input("请输入要购买的物品编号:")
        if buy == 'quit':
            break
        if buy not in product.keys():
            print("输入有误,请重新输入")
            continue

        Input = input("请输入要购买的数量:")
        if Input.isdigit():
            count = float(Input)
        else:
            print("输入有误,请重新输入")
            continue

        have_money = have_money - product[buy]['price'] * count
        if have_money < 0:
            have_money = have_money + product[buy]['price'] * count
            print(f"所需价格:{product[buy]['price'] * count},余额为:{have_money}")
            print("余额不足不可以购买")
        else:
            print(f"你的余额为{have_money}")
            sum_price = product[buy]["price"] * count + sum_price
            buy_pro.append([product[buy]['name'], count])


#结算功能
def calculate():
    button = input("购买结束,是否选择支付,输入yes付款,no退出:")
    if button == "no":
        return 0
    else:
        user[username]["money"] -= sum_price
        print("支付成功!")
        print(f"余额:{user[username]['money']}")



while 1:
    username = input("请输入用户名:")
    if username == 'quit':
        break
    pwd = input("请输入密码:")
    if username not in user.keys():
        print("用户名不存在")
        continue

    elif user[username]['passwd'] == pwd:
        print("登录成功!")
        have_money = user[username]['money']
        print("-" * 50)
        buy_pro = []
        sum_price = 0.0
        menu()
        while 1:

            b = input("请选择你要进行的操作:")
            if b == '1':
                product_list()
            elif b == '2':
                cart()
            elif b == '3':
                buy()
            elif b == '4':
                calculate()
            elif b == '5':
                print("成功退出!")
                break
        break
    else:
        print("密码错误")
        continue




实现效果:
--------------------------------------------------
#                欢迎登录三乐购物系统                #
--------------------------------------------------
温馨提示,输入quit退出系统
请输入用户名:root
请输入密码:123
登录成功!
--------------------------------------------------
------------------菜单栏-------------------
                 输入1查看商品列表                 
                 输入2查看购物车                  
                 输入3进行购买                   
                 输入4结算                     
                 输入5退出系统                   
-------------------------------------------
请选择你要进行的操作:1
商品列表
编号:f001,名称:apple,单价:2.5
编号:f002,名称:banana,单价:3.5
编号:f003,名称:grape,单价:4.5
编号:f004,名称:fork,单价:18
编号:f005,名称:sheep,单价:25
请选择你要进行的操作:

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值