day4_shop程序

# 1、作业:
# 先让你登录
# {
# "nhy":{"passwd":123,'role':1,'money':10000,"carts":['mac']},
# "nhy2":{"passwd":123,'role':2,'money':10000,"carts":[]}
# }
# role是1的话,代表管理员,2代表普通用户
# 需求分析
# 1、存用户信息的这个文件、存商品信息的文件,都需要初始化
# 2、登录
# 3、登录成功之后判断角色展示不同的菜单

#普通用户:
# 1、查看所有商品,打印字典 2、购买商品,字典的修改,修改完之后写文件 3、查看购物车和余额,打印字典 4、退出
#管理员:
# 1、添加商品,字典的修改,修改完之后写文件 2、充值、输入用户名,字典的修改,修改完之后写文件 3、退出
# 公共的,就是读文件、写文件
# user.txt = {
'ssj': {'password': '123456', 'role': 1, 'money': 10000.0, 'carts': ['mac']},
'hua': {'password': '123456', 'role': 2, 'money': 100000.0, 'carts': []}
}
# goods.txt = {
'mac': 5000,
'iphone': 9000,
'car': 20000
}

USER_FILE = 'user.txt'  # 用常量保存用户信息
GOODS_FILE = 'goods.txt' # 用常量保存商品信息


def op_file(file_name, content=None): # content=None,如果content为真是写操作
f = open(file_name, 'a+')
f.seek(0)
if content:
f.truncate()
f.write(str(content))
f.flush()
res = None
else:
res = eval(f.read())
f.close()
return res


def not_null(var): # 输入的内容不为空的函数
if len(var) > 0:
return True
return False


def is_price(s): # 合法的价格函数
s = str(s)
if s.count('.') == 1:
new_s = s.split('.')
left_num = new_s[0]
right_num = new_s[-1]
if right_num.isdigit():
if left_num.isdigit():
return True
elif s.isdigit() and int(s) > 0:
return True
return False


def add_products(): # 管理员添加商品
products = op_file(GOODS_FILE)
while True:
product_name = input('请输入你要添加的商品名称:').strip()
money = input('请输入你的商品价格:').strip()
if not_null(product_name) and not_null(money):
if product_name not in products:
if is_price(money): # 价格合法的话
products[product_name] = float(money)
op_file(GOODS_FILE, products)
print('商品添加成功!')
return admin() # 添加成功,都会返回上级菜单
else:
print('价格输入不合法!')
else:
print('商品已存在!')
else:
print('商品名称/价格不能为空!')


def recharge(): # 管理员充值
users = op_file(USER_FILE)
while True:
username = input('请输入你要充值的账号:').strip()
money = input('请输入你要充值的钱:').strip()
if not_null(username) and not_null(money):
if username in users:
if is_price(money):
src_money = users[username]['money']
users[username]['money'] = src_money + float(money)
op_file(USER_FILE, users)
print('充值成功!')
return admin() # 充值成功,都会返回上级菜单
else:
print('钱输入不合法!')
else:
print('用户不存在!')

else:
print('账号/钱不能为空!')


def admin(): # 管理员菜单函数
choice = input('请输入你的选择:1、充值 2、添加商品 3、退出').strip()
menu = {
'1': recharge,
'2': add_products,
'3': exit
}
if choice in menu:
menu[choice]()
else:
print('请输入正确的选择!')
admin() # 输入不正确,再调用一下自己重新输入


def show_products(user): # 普通用户查看商品,要传入一个user,哪个账号查看商品
products = op_file(GOODS_FILE)
for k in products:
print('商品名称是%s,价格是%s' % (k, products[k]))
buyer(user)


def buy(user): # 普通用户购买商品,要传入user
# 1、把原来的用户信息读出来
# 2、买完之后,再写进去
users = op_file(USER_FILE)
products = op_file(GOODS_FILE)
money = users[user]['money']
carts = users[user]['carts']
for k in products:
print('商品名称是%s,价格是%s' % (k, products[k]))
while True:
product_name = input('请输入你要购买的商品名称:').strip()
if not_null(product_name):
if product_name in products:
product_price = products[product_name]
if money >= product_price:
carts.append(product_name)
money -= product_price
users[user]['carts'] = carts
users[user]['money'] = money
op_file(USER_FILE, users)
print('商品购买成功!')
buyer(user)
else:
print('对不起,你的余额不足,你的余额是 %s' % money)
else:
print('你输入的商品名称不存在!')
else:
print('商品名称不能为空!')


def user_info(user): # 把哪个用户的信息打印出来
users = op_file(USER_FILE)
money = users[user]['money']
carts = users[user]['carts']
print('你的余额是%s' % money)
print('你的购物车里有', carts)
buyer(user)


def buyer(user): # 普通用户菜单函数
choice = input('请输入你的选择:1、查看商品信息 2、购买商品 3、查看购物车 4、退出').strip()
menu = {
'1': show_products,
'2': buy,
'3': user_info,
'4': exit
}
if choice in menu:
menu[choice](user)
else:
print('请输入正确的选择!')
buyer(user)


def login(): # 登录函数读文件
users = op_file(USER_FILE)
for i in range(3):
username = input('请输入账号:').strip()
password = input('请输入密码:').strip()
if not_null(username) and not_null(password): # 账号和密码都不为空
if username in users:
cur_user = users[username] # 代表通过账号获取到的小字典
if cur_user['password'] == password:
if cur_user['role'] == 1:
print('欢迎管理员登录')
admin()
else:
print('欢迎%s登录' % username)
buyer(username)
else:
print('用户不存在!')
else:
print('账号/密码不能为空!')


login()
原文地址:https://www.cnblogs.com/laosun0204/p/8515287.html