python实现简单购物系统
购物系统简单功能
使用系统需要
登录验证
,进入系统后,
输入购物预算
信息
•
用户可以任意查看商品信息和已购买信息
•
用户根据商品编号购买商品,购买时,检测余额是否够,够就直接扣款,不够可以充值
•
可随时退出(输入exit),退出后,打印已购买商品和余额
•
要求:用尽可能友好的方式输出(作业需要提交代码及执行结果截图)
封装函数
{'F00001': {'name':'苹果', 'price':1.2},
'F00002': {'name':'香蕉', 'price':5.5}, }
功能比较简单,以后学了更多知识后再来补充功能
"""
@name : 三乐购物系统.py
@author : wangshenghu
@projectname: tlbb
@Date : 2022/1/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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通