#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-04-29 12:16:23
# @Author : hjc (284738209@qq.com)
# @Link : http://www.cnblogs.com/tootooman/
# @Version : 2
import os
import re
import time
import json
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
]
userdb = 'userdb'
if not os.path.isfile(userdb):
db = open(userdb, 'w', encoding='utf8')
db.write('{}')
db.close()
shopping_car = {}
flag = True
while flag:
username = input('用户名:').strip()
password = input('密码:').strip()
with open(userdb, 'r+') as users:
users_data = json.load(users)
if username in users_data:
if password == users_data[username]:
# os.path.isfile(username)
check = input('是否要查看消费历史[y:查看/任意键:进入商城]:').strip()
f = open(username, 'r', encoding='utf8')
if check == 'y':
print('start'.center(60, '-'))
for line in f:
print(line)
print('end'.center(60, '-'))
ret = ''
f.seek(0)
for line in f:
ret = re.findall('余额.*', line)
salary = int(ret[0].split(':')[-1])
f.close()
break
else:
print('用户名或密码错误。')
else:
print('你不是会员!系统已自动帮你注册会员!')
users.seek(0)
users_data[username] = password
json.dump(users_data, users)
while 1:
salary = input('兜里有多少钱::').strip()
if salary.isdigit():
salary = int(salary)
f = open(username, 'w', encoding='utf8')
f.close()
flag = False
break
else:
print('你兜里的不是钱!')
while 1:
title = '''
%s
%s
%s
''' % (''.center(60, '*'), '商品列表'.center(60, ' '), ''.center(60, '*'))
print(title)
for i in range(len(goods)):
info = '''
商品编号:%s 商品名称:%s 商品价格:%s
''' % (i, goods[i]['name'], goods[i]['price'])
print(info)
choice = input('输入商品编号[q:结算]:').strip()
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(goods):
if salary >= goods[choice]['price']:
goods_name = goods[choice]['name']
goods_price = goods[choice]['price']
salary -= goods_price
if shopping_car.get(goods_name):
shopping_car[goods_name]['total'] += 1
print('