05 - 练习:购物车
需求:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
流程图
# -*- coding:utf-8 -*-
# Author:Zane
'''
程序要求>>>>>
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
'''
product_list = [
('iphone', 5800),
('Mac Pro', 9800),
('bike', 1000),
('watch', 10000),
('Coffee', 30),
('Python', 99)
]
fo = open(r"day01\\userinfo.txt", 'r+', encoding='utf-8') # 打开用户信息文件
fo_r = fo.read() # 读取文件得到字符串
fo_dict = eval(fo_r) # 将字符串数据转化成数组
fo_his = open(r'day01\\histroy.txt', 'r+', encoding='utf-8')
fo_his_r = fo_his.read()
fo_his_dict = eval(fo_his_r)
username = input("请输入用户名:")
password = input("请输入密码:")
shoppinglist = fo_his_dict[username]
shoppinglist_now = []
salary = fo_dict[username][password]
salary = int(salary)
if username in fo_dict:
if password in fo_dict[username]:
print("登陆成功")
# print("工资>>>",str(fo_dict[username][1]))
if fo_dict[username][password] == '':
salary = input("请输入你的工资:")
salary = int(salary)
# 将工资写入文件中
fo_dict[username] = {password: salary}
fo.seek(0)
fo.write(str(fo_dict))
fo.tell()
# 购物程序开始
while True:
for index, item in enumerate(product_list):
# print(product_list.index(item),item)
print(index, item)
user_choice = input("选择您希望购买的商品>>>>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shoppinglist_now.append(p_item)
salary -= p_item[1]
print("将商品%s添加入购物车,你的余额还剩:\033[31;1m%s\033[0m" % (
p_item, salary))
else:
print("\033[41;1m你的余额只有[%s]啦,还买个P\033[0m" %
(salary))
else:
print("商品不存在")
elif user_choice == "q":
print("-----购买的商品-----")
for p in shoppinglist_now:
print(p)
print("你的余额:", salary)
fo_dict[username] = {password: salary}
fo.seek(0)
fo.write(str(fo_dict))
fo.tell()
shoppinglist.extend(shoppinglist_now)
fo_his_dict[username] = shoppinglist
fo_his.seek(0)
fo_his.write(str(fo_his_dict))
fo_his.tell()
exit()
else:
print("错误选项")
else:
print("您的工资余额为:", fo_dict[username][password])
print("------你目前所购买的物品如下------")
for p2 in shoppinglist:
print(p2)
print("您的余额为:", fo_dict[username][password])
print("-----请继续购物------")
while True:
for index, item in enumerate(product_list):
# print(product_list.index(item),item)
print(index, item)
user_choice = input("选择您希望购买的商品>>>>>>:")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
if p_item[1] <= salary:
shoppinglist_now.append(p_item)
salary -= p_item[1]
print("将商品%s添加入购物车,你的余额还剩:\033[31;1m%s\033[0m" % (
p_item, salary))
else:
print("\033[41;1m你的余额只有[%s]啦,还买个P\033[0m" %
(salary))
else:
print("商品不存在")
elif user_choice == "q":
print("-----购买的商品-----")
for p in shoppinglist_now:
print(p)
print("你的余额:", salary)
print("--------结束--------")
fo_dict[username] = {password: salary}
fo.seek(0)
fo.write(str(fo_dict))
fo.tell()
shoppinglist.extend(shoppinglist_now)
fo_his_dict[username] = shoppinglist
fo_his.seek(0)
fo_his.write(str(fo_his_dict))
fo_his.tell()
exit()
else:
print("错误选项")
fo.close()
fo_his.close()
else:
print("输入的用户名或密码错误")