标签:python
思路:
1.首先生成一个用户字典文件,元素包含用户名,密码,余额,购物车清单、数量
2.导入字典内容,用于验证登录和记录用户信息
3.首页可以选择登录、注册和浏览商品列表
4.注册模块,要根据字典key值判断用户是否存在,存在就返回,不存在创建,密码字数限制没有做,getpass模块在window平台报错,没有写
5.登录模块,根据字典内容验证,验证成功登录,不成功询问是否注册,三次验证失败退出程序
6.登录成功后,判断用户字典中是否存在余额,不存在则要求用户输入金额,并保存在字典中
7.打印商品列表,根据用户选择:退出程序、注销用户、检查购物车、充值、购买功能
8.购物的一些功能没有深入做,例如添加到购物车后取消购买,购物数量选择等等,待优化
9.退出程序的时候把新产生的用户信息保存到用户字典文件
第一次需要把字典内容写入文件:
# -*- coding: utf-8 -*-
import pickle
user_list = {
‘test1‘:{
‘username‘:‘test1‘,
‘userpasswd‘:‘123456‘,
‘salary‘:‘‘,
‘shop_car‘:‘‘,
‘shop_car_list‘:‘‘
},
‘test‘:{
‘username‘:‘test‘,
‘userpasswd‘:‘abcd‘,
‘salary‘:‘‘,
‘shop_car‘:‘‘,
‘shop_car_list‘:‘‘
}
}
f = open(‘E:/linux 学习/python学习/day2/user_list.txt‘,‘wb‘)
pickle.dump(user_list,f)
f.close()# -*- coding: utf-8 -*-
import pickle
f = open(‘E:/linux 学习/python学习/day2/user_list.txt‘,‘rb‘)
user_list = pickle.load(f)
f.close()
def wirte_logout():
f = open(‘E:/linux 学习/python学习/day2/user_list.txt‘,‘wb‘)
pickle.dump(user_list,f)
f.close()
return
product_list = [
(‘Iphone 6s‘,5299),
(‘Iphone 6s Plus‘,5999),
(‘Sumsung s7‘,3888),
(‘Sumsung s7 Edge‘,5688),
(‘360 F4 ‘,799),
(‘红米Note3 ‘,899),
(‘魅族 MX6‘,1999),
(‘华为 Mate‘,2799),
]
def index_page():
page = ‘‘‘
[0]登录
[1]注册
[2]浏览
‘‘‘
print(‘\n‘,‘测试页面‘.center(60,‘-‘),page)
return
def login():
print(‘\n‘)
global user
count = 1
for i in range(5):
if count <= 3:
user = input(‘用户登录: ‘)
password = input(‘密 码: ‘)
if user in user_list.keys():
if user == user_list[user][‘username‘] and password == user_list[user][‘userpasswd‘]:
msg = ‘‘‘
欢迎登录!
‘‘‘
print(msg)
break
else:
print(‘用户名或密码不正确,请重新登录!‘)
else:
user_choice = input(‘用户不存在,是否需要注册(y/n)‘)
if user_choice == ‘y‘:
add_user()
break
elif user_choice == ‘n‘:
pass
count += 1
elif count > 3:
exit(‘超出登录次数!登录失败‘)
return
def add_user():
global user
exit_flag = False
print(‘\n‘)
while not exit_flag:
username = input(‘请输入你的用户名:‘)
if username in user_list.keys():
print(‘\n用户名已存在,请输入其他名称\n‘)
else:
exit_flag = True
userpasswd = input(‘请输入你的密码‘)
user_list[username] = {}
user_list[username][‘username‘] = username
user_list[username][‘userpasswd‘] = userpasswd
user_list[username][‘salary‘] = ‘‘
user_list[username][‘shop_car‘] = ‘‘
user_list[username][‘shop_car_list‘] = ‘‘
print(‘\n注册成功,你的用户名是:%s‘ % username,‘\n‘)
user = user_list[username][‘username‘]
return
def print_product_list():
print(‘\n产品列表:\n‘)
for item in enumerate(product_list):
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print(index,‘:‘,p_name,p_price)
return
def printending():
print(‘你之前购买的商品‘.center(50,‘-‘))
for item in user_list[user][‘shop_car‘]:
a = user_list[user][‘shop_car‘].index(item)
print(‘商品:%s 价格:%s 数量:%s‘ % (user_list[user][‘shop_car‘][a][0],user_list[user][‘shop_car‘][a][1],user_list[user][‘shop_car_list‘][a]))
print(‘End‘.center(58,‘-‘))
print(‘你的余额是:%s‘ % user_list[user][‘salary‘],‘\n‘)
return
exit_flag = False
while not exit_flag:
index_page()
index_user_choice = input(‘请输入您要进行的操作:‘)
if index_user_choice == ‘0‘:
login()
exit_flag = True
elif index_user_choice == ‘1‘:
add_user()
exit_flag = True
elif index_user_choice == ‘2‘:
print_product_list()
else:
print(‘输入操作无效!‘)
print(‘Begin The Shopping‘.center(80,‘-‘),‘\n‘)
exit_flag2 = False
while not exit_flag2:
if not user_list[user][‘salary‘]:
salary = input(‘你的余额为0,请充值金额: ‘)
if salary.isdigit():
salary = int(salary)
user_list[user][‘salary‘] = salary
else:
print(‘数据类型不正确,请输入一个正确的数字!‘)
continue
else:
salary = user_list[user][‘salary‘]
if not user_list[user][‘shop_car‘]:
shop_car = []
shop_car_list = []
else:
shop_car = user_list[user][‘shop_car‘]
shop_car_list = user_list[user][‘shop_car_list‘]
print_product_list()
print(‘‘‘
[q=quit,c=check,l=logout,r=rechage]
‘‘‘)
user_choice = input(‘请问你想购买什么?‘)
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary:
if p_item not in shop_car:
shop_car.append(p_item)
shop_car_list.append(1)
user_list[user][‘shop_car‘] = shop_car
user_list[user][‘shop_car_list‘] = shop_car_list
salary -= p_item[1]
user_list[user][‘salary‘] = salary
print(‘在购物车上添加了:%s,价格是:%s,你的余额是:%s‘ % (p_item[0],p_item[1],user_list[user][‘salary‘]),‘\n‘)
else:
item_num = shop_car_list[shop_car.index(p_item)]+1
shop_car_list[shop_car.index(p_item)] = item_num
user_list[user][‘shop_car_list‘] = shop_car_list
salary -= p_item[1]
user_list[user][‘salary‘] = salary
print(‘在购物车上添加了:%s,价格是:%s,你的余额是:%s‘ % (p_item[0],p_item[1],user_list[user][‘salary‘]),‘\n‘)
else:
print(‘你的余额是:%s,余额不足购买!‘ % user_list[user][‘salary‘],‘\n‘)
else:
print(‘你选择的商品不存在!‘,‘\n‘)
else:
if user_choice == ‘q‘ or user_choice == ‘quit‘:
printending()
print(‘Bye %s‘ % user)
wirte_logout()
exit_flag2 = True
elif user_choice == ‘c‘ or user_choice == ‘check‘:
printending()
elif user_choice == ‘l‘ or user_choice == ‘logout‘:
printending()
login()
elif user_choice == ‘r‘ or user_choice == ‘rechage‘:
salary_change = input(‘请输入充值金额:‘)
if salary_change.isdigit():
salary_change = int(salary_change)
salary += salary_change
user_list[user][‘salary‘] = salary
else:
print(‘数据类型不正确,请输入一个正确的数字!‘)
continue
else:
print(‘请输入合法字符!‘,‘\n‘)观看视频学习自行练习,有不足的地方请见谅。
本文出自 “ld0381的学习之旅” 博客,请务必保留此出处http://ld0381.blog.51cto.com/3318114/1835396
标签:python
原文地址:http://ld0381.blog.51cto.com/3318114/1835396