1.Python-简单购物系统

目录

一、三乐购物系统

1.1 简单功能


一、三乐购物系统

1.1 简单功能

使用系统需要 登录验证 ,进入系统后, 输入购物预算信息
• 用户可以任意查看商品信息和已购买信息
• 用户根据商品编号购买商品,购买时,检测余额是否够,够就直接扣款,不够可以充值
• 可随时退出(输入exit),退出后,打印已购买商品和余额
• 要求:用尽可能友好的方式输出(作业需要提交代码及执行结果截图)
封装函数
{'F00001': {'name':'苹果', 'price':1.2},
'F00002': {'name':'香蕉', 'price':5.5}, }

"""
@name : 三乐购物系统.py
@author : Zengkaijie
@projectname: Python
@Date  : 2022/2/24
"""
"""
 购物系统分析:
 1.菜单界面,用函数封装一个菜单 menu 
     菜单选项应该有登录,注册,退出功能
 2.登录界面,验证密码,成功进入购物界面,失败提醒,
 3.购物界面,用户根据商品编号购买商品,检测余额是否足够,够就直接扣钱,不够就提醒
 4.结算界面,打印已经购买商品,显示余额
 5.充值界面,余额不足进行选择充值还是退出
 6.退出函数,exit
"""
import sys
 
# 定义几个用户信息
user = {'root': {'pd': '123456', '余额': '200'}, 'wangshenghu': {'pd': '456789', '余额': '10000'}}
 
 
# 登录函数
def login():
    global username, passwd
    username = input("请输入用户名:")
    if username == 'exit':
        exit()
    else:
        passwd = input("请输入密码:")
        if username in user.keys() and passwd == user[username]['pd']:
            print("登录成功!")
            print("您当前余额为:", user[username]['余额'])
            return shoping()
        else:
            print("用户名或者密码输入错误!请重新输入.")
            return login()
 
 
# 注册函数
def register():
    # 定义全局变量,否则在其他函数不能调用
    global username, passwd, money
    print("欢迎注册三乐购物系统")
    username = input("请输入注册用户名 : ")
    if username == 'exit':
        exit()
    # 判断用户是否存在重复
    else:
        if username in user.keys():
            print("用户已经存在,请重新输入!")
            return register()
        else:
            passwd = input("请输入密码 : ")
            if passwd == 'exit':
                exit()
            else:
                money = float(input("请输入您的充值金额 : "))
                if money <= 0:
                    print("您的充值金额小于0元,请重新注册!")
                    return register()
                else:
                    user.update({username: {'pd': passwd, '余额': money}})
                    print("注册成功!")
                    return munu()
 
 
# 定义商品列表
shoplist = {'F001': {'name': '苹果', 'price': '1.2'},
            'F002': {'name': '香蕉', 'price': '5.5'},
            'F003': {'name': '砂糖橘', 'price': '3.1'},
            'F004': {'name': '柚子', 'price': '10.5'},
            'F005': {'name': '奥迪A8', 'price': '888888'}
            }
# 定义购买列表
buylist = {}
 
# 定义总价格
sumprice = 0
 
 
# 定义充值函数
def topup():
    print("欢迎进入充值系统!")
    upnumber = int(input("请输入您要充值的金额:"))
    user[username]['余额'] = outmoney + upnumber
    print("充值成功!")
    print(f"当前余额:{user[username]['余额']}")
    return shoping()
 
 
# 购物函数
def shoping():
    global sumprice, buylist, outmoney, kindprice
    print(""""
    #######################################################################
    =========================/今日商品信息/=================================
                       {'F001':{'name':'苹果','price':'1.2'}}
                       {'F002':{'name':'香蕉','price':'5.5'}}
                       {'F003':{'name':'砂糖橘','price':'3.1'}}
                       {'F004':{'name':'柚子','price':'10.5'}}
                       {'F005':{'name':'AuDi A8 ','price':'888888.8'}}
    """)
    print("输入exit可以退出系统")
    print("按r结账")
 
    while True:
        shopinput = input("请输入你要购买的商品编号:")
        if shopinput == 'exit':
            exit()
        else:
            if shopinput in shoplist.keys():
                sumprice += float(shoplist[shopinput]['price'])
                buylist.update({shoplist[shopinput]['name']:
                                    {'单价': shoplist[shopinput]['price'],
                                     '总价': sumprice}})
                print("我的购物车:")
                # 输出每一次购物信息
                print(f"购买信息:{shoplist[shopinput]['name']},单价:"
                      f"{shoplist[shopinput]['price']}")
                # a = int(buylist[shopinput]['price'])
                # if shopinput in buylist.keys():
                #     kindprice += a
                #     buylist.update({shoplist[shopinput]['name']:
                #                     {'单价': shoplist[shopinput]['price'],
                #                      '总价': kindprice}})
                # 进入结算功能
            elif shopinput == 'r' or shopinput == 'R':
                print("现在开始结算。")
                print("您今天的购物信息:")
                print(buylist)
                print("需要支付:", sumprice)
                outmoney = int(user[username]['余额'])
                if outmoney >= sumprice:
                    outmoney -= sumprice
                    print("当前账户余额:", outmoney)
                    return exit()
                else:
                    print("您卡里余额不足,是否充值?")
                    print("1.充值余额.")
                    print("2.放弃本次购物。")
                    choise1 = int(input("请您选择: "))
                    # 进入充值系统
                    if choise1 == 1:
                        return topup()
                    elif choise1 == 2:
                        return exit()
                    else:
                        print("输入错误,请重新输入")
 
            else:
                print("输入错误!")
 
 
# 退出函数
def exit():
    # 调用sys库的exit方法
    sys.exit(0)
 
 
# 菜单函数
def munu():
    print("欢迎进入三乐购物系统!")
    print("#####菜单#####")
    print("1.登录")
    print("2.注册")
    print("3.退出")
    choice = input("请您选择:")
    if choice == '1':
        login()
    elif choice == '2':
        register()
    elif choice == '3':
        exit()
    else:
        print("输入错误,请重新输入!")
        return munu()
 
 
munu()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值