03-购物车

进程代码:

a.一共3个文件,其中shopping_user.py为进程代码文件,shopping_user.txt为记录用户相关信息文件,shopping_file.py是购物车目录信息文件.

b.把文件放到同一个目录下,然后执行shopping_user.py脚本,进入用户登录界面,按q可以退出程序,按x可以查询以往的消费清单。用户初始登录次数,工资都为0,初始消费记录为空.

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author:hejianping
#make at 2017-05-03
import getpass
import shopping_file
userfile = open('shopping_user.txt','r')
userdict = {}
new_userlist = []
shopping_list = []

for line in userfile.readlines():
userlist = line.strip().split(':')
_username = userlist[0].strip()
_passwd = userlist[1].strip()
_logincount = int(userlist[2].strip())
_usersalary = int(userlist[3].strip())
_shoppinglist = userlist[4][:].strip()
#使用字典函数让每个用户名生成一个对应的列表:
userdict[_username] = {'name':_username,'pwd':_passwd,'logincount':_logincount,'usersalary':_usersalary,'shoppinglist':_shoppinglist}

while True:
username = input("please input your username:")
if username in userdict.keys():
pass
password = input("请输入密码:")
if password == userdict[username]['pwd'].strip():
if userdict[username]["logincount"] == 0:
print("33[34;1m Welcome user {name} first login !33[0m".format(name=username))
salary = input("please input your salary:")
if salary.isdigit():
salary = int(salary)
else:
print("33[34;1m salary invalid option33[0m")
continue
print("33[34;1m your current balance now is {_salary}33[0m".format(_salary=salary))
else:
print("33[34;1m Welcome user {name} login again!33[0m".format(name=username))
salary = userdict[username]["usersalary"]
print("33[34;1m your current balance now is {_salary}33[0m".format(_salary=salary))

#登录购物:
while True:
print("33[43;1m ---------Shopping list ---------33[0m")
for index, value in enumerate(shopping_file.product):
print(index, value)
print("33[43;1m --------------------------------33[0m")
user_choice = input("What do you want to buy >>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(shopping_file.product) and user_choice >= 0:
price_item = shopping_file.product[user_choice]
if price_item[1] <= salary:
shopping_list.append(price_item)
salary -= price_item[1]
print("33[32;1m Added {_price_item} into shopping cart ,your current balance now is {_salary}33[0m".format(_price_item=price_item, _salary=salary))
else:
print("33[31;1m You had not enough money , your current balance now is {_salary} 33[0m".format(_salary=salary))
else:
print("33[31;1mproduct code {_user_choice} is not exist!33[0m".format(_user_choice=user_choice))
continue
elif user_choice == "q":
userdict[username]['logincount'] += 1
userdict[username]['usersalary'] = salary
#把shopping_list转换为字符窜 追加到客户的购物清单。
userdict[username]['shoppinglist'] += str(shopping_list)
userfile = open('shopping_user.txt', 'w+')
for change in userdict.values():
new_userlist = [str(change['name']), str(change['pwd']), str(change['logincount']),str(change['usersalary']),str(change['shoppinglist'])]
colon = ":"
new_userlist_colon = colon.join(new_userlist)
userfile.write(new_userlist_colon + " ")
print("33[43;1m ---------这次购买的商品有: ---------33[0m")
for product in shopping_list:
print(product)
print("33[32;1m current balance:{_salary}33[0m".format(_salary=salary))
exit()
elif user_choice == "x":
print("33[43;1m ---------以往购买的商品清单: ---------33[0m")
#userdict[username]['shoppinglist'] += str(shopping_list)
print("以往消费记录:{list} ,这次消费记录:{nowlist}" .format(list=userdict[username]['shoppinglist'],nowlist=shopping_list))
else:
print("invalid option")
else:
print("密码输错!")
else:
print("33[31;1m user {user} is not exist!33[0m".format(user=username))

shopping_file.py内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Author:hejianping
#make at 2017-05-03
product = [
("Iphone",3000),
("Watch",1000),
("Bike",800),
("Shoes",500),
("Book",100),
]
shopping_user.txt内容:
hecc:333333:0:0:
hebb:222222:0:0:
heaa:111111:0:0:

  内容格式进行说明:

  heaa:111111:0:0:

  用户名:密码:登录次数:工资:消费记录




原文地址:https://www.cnblogs.com/hejianping/p/6800988.html