#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : user_login_register_shopping.py
# @Author: Anthony.waa
# @Date : 2018/5/23 0023
# @Desc :
# 用户登录状态
flag_user = False
# 用户登录
def login():
'''
:param name: 用户名
:param passwd: 用户登录密码
:return: 实现三次登录成功
'''
count = 1
login_name_passwd = {}
with open('user_pwd.txt', 'r', encoding='utf-8')as f:
for line in f.readlines():
login_name = line.split(',')[0].strip()
login_pwd = int(line.rsplit(',')[-1].strip())
login_name_passwd[login_name] = login_pwd
name = input('请输入用户名:').strip()
passwd = int(input('请输入密码:').strip())
while True:
if name in login_name_passwd and passwd == login_name_passwd.get(name):
flag_user = True
else:
flag_user = False
if flag_user == True:
print('登录成功')
shopping()
else:
print('用户名或密码错误,请重新输入')
name = input('请输入用户名:').strip()
passwd = int(input('请输入密码:').strip())
count += 1
if count == 3:
print('用户名、密码输入错误超过3次,账户已锁定')
exit()
# 用户注册
def register():
'''
:param name: 用户注册
:param passwd: 用户注册使用的密码
:return:
'''
flag_register = True
register_name_passwd = {}
with open('user_pwd.txt', 'r', encoding='utf-8')as f, open('user_pwd.txt', 'a', encoding='utf-8') as f1:
for line in f.readlines():
register_name = line.split(',')[0].strip()
register_passwd = line.split(',')[1].strip()
register_name_passwd[register_name] = register_passwd
name = input('请输入注册用户名:').strip()
passwd = int(input('请输入注册密码:').strip())
while True:
if name in register_name_passwd:
flag_register = True # 该用户已经存在,请重新注册
else:
flag_register = False # 该用户不存在,注册成功
if flag_register == False:
f1.write(name), f1.write(','), f1.write(str(passwd)), f1.write('\n'),
f1.close()
print('用户注册成功')
print('请登录...')
break
else:
print('用户已存在,请重新注册')
name = input('请输入用户名:').strip()
passwd = int(input('请输入密码:').strip())
# 购物车
def shopping():
'''
:return:返回商品清单
'''
# 原始购物清单
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
# 购物车
shopping_car = {}
# 购物车遍历
shopping_car_list = []
# 商品个数
good_count = 1
# 商品单价
shopping_price = {}
# 输入不合法是打印
def return_error():
print("\033[1;31m 输入不合法,请重新输入 \033[0m")
# 打印购物车商品和剩余金额
def end_shopping():
# 获取原始列表中的名称和价格
for list_goods_price in goods:
shopping_price[list_goods_price['name']] = list_goods_price['price']
with open('shopping_list.txt', 'w', encoding='utf-8') as shoppingf:
# 获取购物车中的名称和个数,并从中找到与原始列表中名称所对应的价格
for list_shopping_name, list_shopping_count in shopping_car.items():
shoppingf.writelines('商品名称:{0},商品个数:{1}个,商品价格:{2}元\n'.format(list_shopping_name, list_shopping_count,
shopping_price[list_shopping_name]))
# 格式化购物车商品名称和个数
for list_name in shopping_car:
print('\033[1;32m 商品名称:{0} 商品个数:{1} \033[0m'.format(list_name, shopping_car[list_name]))
print("\033[1;32m 剩余金额为:{0}\033[0m".format(all_money))
while True:
# 用户输入总资产
all_money = input("\033[1;32m 请输入总资产: \033[0m").strip()
if all_money.isdigit():
all_money = int(all_money)
# 显示购物商品列表
print('\033[1;32m ========== 商 品 清 单 ========== \033[0m'.center(20))
for good_index, good in enumerate(goods, 1):
print('\033[1;32m {0} {1} {2} \033[0m'.format(good_index, good['name'],
good['price']).center(20))
print('\033[1;32m 选择"q"退出 \033[0m'.center(20))
while True:
# 选择商品序号,或选择退出购物
choice_count = input("\033[1;32m 请输入商品序号: \033[0m").strip()
if choice_count.isdigit():
choice_count = int(choice_count)
# 商品序号小于商品列表长度
if choice_count > 0 and choice_count <= len(goods):
good_price = goods[choice_count - 1]['price']
# 用户资产大于商品价格,否则提示充值
if all_money > good_price:
good_name = goods[choice_count - 1]['name']
all_money -= good_price
# 商品是否存在购物车中
if good_name not in shopping_car:
shopping_car[good_name] = good_count
else:
for shopping_index, shopping_name in enumerate(shopping_car):
if shopping_name == good_name:
shopping_car[shopping_name] += 1
end_shopping()
else:
print('\033[1;31m 余额不足,请充值: \033[0m'.center(17))
add_money = input("请输入总资产:").strip()
if add_money.isdigit():
add_money = int(add_money)
all_money += add_money
print('\033[1;32m 充值成功,剩余金额为: \033[0m'.center(17), all_money)
continue
elif choice_count.lower() == 'q':
print("\033[1;32m 购物结束,欢迎下次光临 \033[0m")
end_shopping()
exit()
return_error()
# 主体函数
def main_menu():
# 打印选项菜单
print('{0}\n{1}\n{2}\n{3}'.format('选项菜单如下:', '1.注册', '2.登录', '3.购物车'))
while True:
choice = int(input('请输入菜单序号:').strip())
if choice == 1:
print('欢迎进入用户注册界面:')
register()
continue
elif choice == 2:
login()
elif choice == 3:
if flag_user == True:
login()
else:
print('请登录后,再试...')
else:
print('输入有误,请重新输入..')
main_menu()