"""
作者: Tank
创建时间: 2019.07.10 19:05
"""
user_info = {
'user': None,
'pwd': None,
'balance': None
}
import os
# 注册
while True:
user = input('user:')
if os.path.exists(f'{user}.txt'):
print('用户名已存在!')
continue
pwd = input('pwd:')
re_pwd = input('pwd:')
if pwd == re_pwd:
user_info = ','.join([user, pwd, '10000'])
with open(f'{user}.txt', 'w', encoding='utf-8') as f:
f.write(user_info)
f.flush()
print('注册成功!')
break
else:
print('两次密码不一致。')
# 登录
while True:
user = input('user:')
if not os.path.exists(f'{user}.txt'):
print('用户名不存在!')
continue
with open(f'{user}.txt', 'r', encoding='utf-8') as f:
user_content = f.read()
user, file_pwd, balance = user_content.split(',')
pwd = input('pwd:')
if pwd == file_pwd:
user_info['user'] = user
user_info['pwd'] = file_pwd
user_info['balance'] = int(balance)
print('登录成功!')
break
else:
print('密码错误')
'''
1 先循环打印出商品
2 用户输入数字选择商品(判断是否是数字,判断输入的数字是否在范围内)
3 取出商品名,商品价格
4 判断用户余额是否大于商品价格
5 余额大于商品价格时,判断此商品是否在购物车里
5.1 在购物车里,个数加1
5.1 不在购物车里,拼出字典放入({‘good’:{‘price’:10,‘count’:1}})
6 用户余额减掉商品价格
7 花费加上商品价格
8 当输入 q时,购买商品
8.1 消费为0 ,直接退出
8.2 打印购物车
8.3 接受用户输入,是否购买 当输入y,直接调购物接口实现购物
'''
# 购物车
good_list = [
['广东凤爪', 20],
['T-shirt', 150],
['AJ1', 2000],
['macbook pro', 18888],
]
shopping_cart = {}
cost = 0
while True:
if cost:
print(shopping_cart, f'您的购物车清单,总价为{cost}')
sure = input('请确认是否结算: 输入y确认购买,否则退出程序!').strip()
if sure == 'y':
user_info['balance'] -= cost
with open(f'{user_info["user"]}.txt', 'w', encoding='utf-8') as f:
user_info['balance'] = str(user_info['balance'])
res = ','.join([user_info['user'], user_info['pwd'], user_info['balance']])
f.write(res)
f.flush()
print('购买成功, 欢迎下次光临!')
break
else:
cost = 0
print('退户程序!')
break
for index, goods in enumerate(good_list):
print(index, goods)
choice = input('请选择商品编号:').strip()
if not choice.isdigit():
print('输入有误,请输入数字')
continue
choice = int(choice)
good, price = good_list[choice]
print(good, price)
if user_info['balance'] >= price:
if good not in shopping_cart:
shopping_cart[good] = 1
else:
shopping_cart[good] += 1
cost += price
print('添加购物车成功!')