运行后有登录注册和退出
注册成功登录后有选项,充值完成后可以将商品加入购物车然后购买,购买完成后可以选择退出或者继续购买
购物车内可以修改商品和删除商品
购买完成后询问地址和电话
具体可以看图
基本代码已经实现,好用请你采纳:
class User:
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.cart = []
def add_to_cart(self, item, price):
self.cart.append((item, price))
self.balance -= price
def remove_from_cart(self, item):
for i, (name, price) in enumerate(self.cart):
if name == item:
del self.cart[i]
self.balance += price
break
def check_balance(self):
print(f"当前余额为: {self.balance}")
class ShoppingMarket:
def __init__(self):
self.users = {}
self.products = [("item1", 100), ("item2", 200), ("item3", 300)]
def register(self, name, password):
self.users[name] = User(name, 0)
def login(self, name, password):
if name in self.users:
self.current_user = self.users[name]
return True
return False
def add_balance(self, amount):
self.current_user.balance += amount
def show_cart(self):
print("购物车:")
for item, price in self.current_user.cart:
print(f"{item}: {price}")
def show_products(self):
print("所有商品:")
for item, price in self.products:
print(f"{item}: {price}")
def buy(self, item, price):
if self.current_user.balance >= price:
self.current_user.add_to_cart(item, price)
def remove_from_cart(self, item):
self.current_user.remove_from_cart(item)
def checkout(self, address, phone):
print(f"运输{address} with 电话 {phone}")
print("购买的物品:")
for item, price in self.current_user.cart:
print(f"{item}: {price}")
self.current_user.cart = []
self.current_user.check_balance()
def run(self):
while True:
print("1. 注册")
print("2. 登录")
print("3. 退出")
choice = input("请输入选项: ")
if choice == "1":
name = input("请输入用户名: ")
password = input("请输入密码: ")
self.register(name, password)
elif choice == "2":
name = input("请输入用户名: ")
password = input("请输入密码: ")
if self.login(name, password):
while True:
print("1. 查看余额")
print("2. 充值")
print("3. 查看购物车")
print("4. 查看所有商品")
print("5. 添加商品到购物车")
print("6. 从购物车删除商品")
print("7. 结账")
print("8. 退出")
choice = input("请输入选项: ")
if choice == "1":
self.current_user.check_balance()
elif choice == "2":
amount = int(input("请输入充值金额: "))
self.add_balance(amount)
elif choice == "3":
self.show_cart()
elif choice == "4":
self.show_products()
elif choice == "5":
item = input("请输入商品名称: ")
price = int(input("请输入商品价格: "))
self.buy(item, price)
elif choice == "6":
item = input("请输入商品名称: ")
self.remove_from_cart(item)
elif choice == "7":
address = input("请输入地址: ")
phone = input("请输入电话: ")
self.checkout(address, phone)
elif choice == "8":
break
elif choice == "3":
break
if __name__ == "__main__":
ShoppingMarket().run()
根据您描述的问题,希望用python实现网上购物商场系统,这里给您找到一些资料,包含源码,您可以拿来修改修改:
1.基于Python的网上购物商场系统的设计与实现
https://blog.csdn.net/lingpao1688/article/details/120320864
2.https://github.com/mirumee/saleor
3.基于Python的购物商城管理系统
https://github.com/kongxiangchx/Shopping-mall-management-system
import os
# 定义商品列表
goods_list = [
{'name': '电脑', 'price': 1999},
{'name': '鼠标', 'price': 10},
{'name': '游艇', 'price': 20},
{'name': '美女', 'price': 998},
]
# 定义购物车
shopping_cart = []
# 定义用户信息
user_info = {
'name': '',
'balance': 0,
'address': '',
'phone': ''
}
# 注册
def register():
print('欢迎注册')
name = input('请输入用户名:')
balance = int(input('请输入余额:'))
user_info['name'] = name
user_info['balance'] = balance
print('注册成功!')
# 登录
def login():
print('欢迎登录')
name = input('请输入用户名:')
if name == user_info['name']:
print('登录成功!')
return True
else:
print('用户名不存在!')
return False
# 充值
def recharge():
print('欢迎充值')
money = int(input('请输入充值金额:'))
user_info['balance'] += money
print('充值成功!')
# 查看商品
def show_goods():
print('商品列表如下:')
for index, item in enumerate(goods_list):
print(index, item['name'], item['price'])
# 添加商品
def add_goods():
show_goods()
index = int(input('请输入要购买的商品编号:'))
item = goods_list[index]
if item['price'] <= user_info['balance']:
shopping_cart.append(item)
user_info['balance'] -= item['price']
print('添加商品成功!')
else:
print('余额不足!')
# 查看购物车
def show_shopping_cart():
print('购物车内容如下:')
for index, item in enumerate(shopping_cart):
print(index, item['name'], item['price'])
# 修改购物车
def modify_shopping_cart():
show_shopping_cart()
index = int(input('请输入要修改的商品编号:'))
item = shopping_cart[index]
print('原商品:', item['name'], item['price'])
new_item = goods_list[int(input('请输入新的商品编号:'))]
if new_item['price'] <= user_info['balance']:
shopping_cart[index] = new_item
user_info['balance'] -= new_item['price']
print('修改商品成功!')
else:
print('余额不足!')
# 删除购物车
def delete_shopping_
可以找万能的淘宝或者pdd看一下,或者找下csdn的资源。
仅供参考,部分代码:
#存放已有的原始用户
user = {'root': {'passwd': '123456', '余额': 300},
'admin': {'passwd': '123123', '余额': 400}}
#存放商品信息
dict = {'F001': {'name': '苹果', 'price': 4.2, 'count': 100}, 'F002': {'name': '香蕉', 'price': 3.2, 'count': 100},
'F003': {'name': '棉花糖', 'price': 10, 'count': 100},
'F004': {'name': '饼干', 'price': 5.2, 'count': 100}, 'F005': {'name': '芒果', 'price': 9.0, 'count': 100},
'F006': {'name': '鸡蛋', 'price': 3.0, 'count': 100},
'F007': {'name': '果冻', 'price': 3.2, 'count': 100}, 'F008': {'name': '辣条', 'price': 3.5, 'count': 100},
'F009': {'name': '牛奶', 'price': 5.0, 'count': 100}}
#注册函数
def register(uname1, upasswd1, umoney):
if umoney >= 100:
user.update({uname1: {'passwd': upasswd1, '余额': umoney}})
print(f"亲爱的{uname1},恭喜您注册成功!您的账户余额为{umoney},赶紧去登陆吧!")
elif umoney < 100:
print(f"您充值的金额低于100,注册失败,请重新注册")
#登陆函数
def login(uname2,upasswd2):
global c
if upasswd2 == user[uname2]['passwd']:
print(f"欢迎{uname2}用户登陆成功!您的账户余额为{user[uname2]['余额']}")
c = 1
elif uname2 in user and upasswd2 != user[uname2]['passwd']:
print(f"抱歉!亲爱的{uname2},您的密码输入错误!请重新输入!您还有{3 - i}次机会")
c = 0
#购买商品函数
def shop():
print("购买界面".center(100, '*'))
sig2 = input("请将您选中的商品编号输入在此(退出请按'q'):")
if sig2 in dict:
sig3 = input(f"请将{dict[sig2]['name']}的购买数量输入在此:")
if sig3.isdigit():
sig3 = int(sig3)
if sig3 <= dict[sig2]['count']:
total = sig3 * dict[sig2]['price']
if total <= user[uname2]['余额']:
umoney = user[uname2]['余额'] - total
user.update({uname2: {'passwd': upasswd2, '余额': umoney}})
dict.update({sig2:{'name':dict[sig2]['name'],'price':dict[sig2]['price'],'count':dict[sig2]['count']-sig3}})
shopcar.append({'商品名称': dict[sig2]['name'], '购买数量': sig3})
print(f"已购买{sig3}个{dict[sig2]['name']},花费{total}元,您的余额为{umoney} ")
else:
print("抱歉!您的余额不足,不能进行购买!请充值")
return recharge()
else:
print("抱歉,本商品仓库数量不足")
else:
print("您输入的购买数量有误!请重新输入")
return shop()
elif sig2 == 'q':
pass
else:
print("您输入的编号有误!请重新输入!")
return shop()
#充值函数
def recharge():
print("充值界面".center(100, '*'))
print(f"亲爱的{uname2}用户,目前您的账户余额为 :{user[uname2]['余额']}元")
r_moeny = input("请输入您要充值的金额(退出请按q):")
if int(r_moeny) < 50:
print("充值金额不得低于50哦!")
return recharge()
if r_moeny == 'q':
pass
else:
r_moeny = float(r_moeny)
umoney = r_moeny + user[uname2]['余额']
for k in range(1,4):
upasswd3 = input("请输入您的登陆密码进行验证: ")
if upasswd3 == user[uname2]['passwd']:
user.update({uname2: {'passwd': upasswd2, '余额': umoney}})
print(f"恭喜您,充值成功,目前您的账户余额为:{user[uname2]['余额']}元")
break
else:
print(f"抱歉!亲爱的{uname2},您的密码输入错误!充值失败!请重新输入!您还有{3 - k}次机会")
#购物清单函数
def shop_car():
print("购物清单界面".center(100, '*'))
if shopcar == [] :
print("小主,这里空空如也,赶紧到三乐购物商城去选购商品吧!")
else:
print("您的购物清单如下:")
for j in shopcar:
print(j)
#主菜单
count = 0
while count == 0:
print("欢迎来到三乐购物系统!".center(100, '-'))
print("1.注册".center(80))
print("2.登陆".center(80))
print("3.退出".center(80))
option = input("请输入您的选择: ")
if option == '1':
print("登陆界面".center(100, '-'))
uname1 = input("请设置您的用户名(请将用户名设置为3-10个字符串的小写字母): ")
if uname1 in user.keys():
print("用户名已经存在,请重新注册!")
elif uname1.islower() and 2 < len(uname1) < 11:
upasswd1 = input("请设置您的密码:(请将密码设置为6位数字) ")
if upasswd1.isdigit() and len(upasswd1) == 6:
umoney = input("请输入您要充值的金额(初次充值不得低于100):")
umoney = float(umoney)
register(uname1, upasswd1, umoney)
else:
print("您设置的密码不符合规范!注册失败,请重新注册")
else:
print("您设置的用户名不符合规范,请重新设置!")
elif option == '2':
print("注册界面".center(100, '-'))
flag = 0
while flag == 0:
uname2 = input("请输入您注册的用户名: ")
if uname2 not in user and uname2 == 'new come':
flag = 1
elif uname2 not in user:
print(f"抱歉!{uname2} 此用户名不存在!请重新输入或者注册!输入'new come'进入菜单页面")
flag = 0
continue
elif uname2 in user:
for i in range(1, 4):
upasswd2 = input("请输入您的密码: ")
login(uname2,upasswd2)
if c == 1:
flag = 1
count = 1
shopcar = [] #用列表存放用户已购买的商品
while 1:
print("三乐购物系统".center(100, '#'))
print("1、查看商品".center(80))
print("2、账户充值".center(80))
print("3、购买商品".center(80))
print("4、查看购物清单".center(80))
print("5、退出系统".center(80))
option2 = input("请输入您的选择:")
if option2 == '1':
print("目前三乐购物系统中有的商品信息如下".center(100, "#"))
for i in dict:
print(i, end=': ')
print(dict[i])
elif option2 == '2':
recharge()
elif option2 == '3':
shop()
elif option2 == '4':
shop_car()
elif option2 == '5':
print("\033[1;36m三乐购物系统欢迎您的下次光临!\033[0m")
exit()
else:
print("您的输入有误!请重新输入!")
print("请重新登陆!或者输入'new come'进入菜单页面".center(100, '-'))
elif option == '3':
print("\033[1;36m三乐购物系统欢迎您的下次光临!\033[0m")
exit()
else:
print("您的输入有误,请重新输入!")