def buy(): db = common.conn_db() money = db.get(current_user['user']).get('money') print('你有%d元' % money) goods_dict = {'apple': 1, 'banana': 1} print(goods_dict.keys()) goods_bought_dic = {} while True: input_str = input('buy goods(such as apple) number(such 5) ') input_str_split = input_str.split(' ') if len(input_str_split) == 1 and input_str_split[0] == 'q': db[current_user['user']]['money'] = money common.save_db(db) print('退出购物,你买了:') print(goods_bought_dic) print('你还有%d元' % money) break elif len(input_str_split) == 3 and input_str_split[ 0] == 'buy' and input_str_split[1] in goods_dict: goods, number = input_str_split[1], input_str_split[2] if goods in goods_dict: goods_price = goods_dict[input_str_split[1]] * int( input_str_split[2]) if goods_price <= money: money -= goods_price print('买成功,你还有%d' % (money)) if goods in goods_bought_dic: goods_bought_dic[goods] += number else: goods_bought_dic[goods] = number else: print('你没有足够的钱') else: print('请输入:\'buy goods(such as apple) number(such 5)\' or \'q\' ')
def buy(): db=common.conn_db() money=db.get(current_user['user']).get('money') print('目前账户有%d元' %money) items_dict={'item1':1,'item2':2} print(items_dict.keys()) items_bought_dic={} while True: item_buy=input('buy which(Q退出)?>>').strip() item_buy_split=item_buy.split(' ') # print(item_buy_split[0],item_buy_split[1]) if item_buy_split[0] in ['q','Q']: db[current_user['user']]['money']=money common.save_db(db) print('你买了:',items_bought_dic) print('你买了:',money) break elif item_buy_split[0] in items_dict: item,item_num=item_buy_split[0],item_buy_split[1] item_price=items_dict[item]*int(item_num) print(item,':'item_num,'共花了%d'%item_price) if item_price<=money: money-=item_price print('购买成功,还有%d元'%money) if item in items_bought_dic: items_bought_dic[item]+=item_num else: items_bought_dic[item]=item_num else: print('余额不足') else: print('请输入【商品名称】【商品数量】')
def deposit_money(): db = common.conn_db() money = db.get(current_user['user']).get('money') print('你有%d元' % money) deposit_money_count = int(input('存多少钱? ')) money += deposit_money_count db[current_user['user']]['money'] = money common.save_db(db) print('存成功,你还有%d' % (money))
def repay(): db=common.conn_db() money=db.get(current_user['user']).get('money') print('账户余额%d元' %money) repay_num=int(input('还款数量?')) money+=repay_num db[current_user['user']]['money']=money common.save_db(db) print('还款成功,目前账户余额%d元:',%money)
def withdraw_money(): db = common.conn_db() money = db.get(current_user['user']).get('money') print('你有%d元' % money) withdraw_money_count = int(input('取多少钱? ')) if withdraw_money_count <= money: money -= withdraw_money_count db[current_user['user']]['money'] = money common.save_db(db) print('取成功,你还有%d' % (money)) else: print('你没有足够的钱')
def withdraw(): db=common.conn_db() money=db.get(current_user['user']).get('money') print('账户余额%d元' %money) withdraw_num=int(input('取多少钱?')) if withdraw_num<=money: money-=withdraw_num db[current_user['user']]['money']=money common.save_db(db) print('取现成功,账户余额%d元:',%money) else: print('账户余额不足')
def wrapper(*args, **kwargs): if current_user['user']: return func(*args, **kwargs) name = input('username>>: ').strip() password = input('password>>: ').strip() db_obj = common.conn_db() #连接数据库,拿到数据库对象 if db_obj.get(name) and password == db_obj.get(name).get('password'): logger1.info('登录成功') logger2.info('登录成功') current_user['user'] = name return func(*args, **kwargs) else: logger1.error('登录失败') logger2.error('登录失败')
def wrapper(*args, **kwargs): if current_user['user']: interval = time.time() - current_user['login_time'] if interval < current_user['timeout']: return func(*args, **kwargs) name = input('name>>: ') password = input('password>>: ') db = common.conn_db() if db.get(name): if password == db.get(name).get('password'): logger.info('登录成功') current_user['user'] = name current_user['login_time'] = time.time() return func(*args, **kwargs) else: logger.error('用户名不存在')
def wrapper(*args, **kwargs): if current_user['user']: interval = time.time() - current_user['login_time'] if interval < current_user['timeout']: return func(*args, **kwargs) name = input('name>>: ') db = common.conn_db() if db.get(name): # 登录 if db.get(name).get('locked') == 1: logger.warning('您被锁定了') print('您被锁定了') else: user_login_error_time = 0 while True: if user_login_error_time >= 3: logger.warning('错误3次,您被锁定了') print('错误3次,您被锁定了') db[name]['locked'] = 1 common.save_db(db) break password = input('password>>: ') if password == db.get(name).get('password'): logger.info('登录成功') print('登录成功') current_user['user'] = name current_user['login_time'] = time.time() return func(*args, **kwargs) else: logger.warning('密码错误') user_login_error_time += 1 else: # 注册 register_flag = input('用户名不存在,是否注册? 是(Y or y) 否(others)\n') if register_flag in ['Y', 'y']: password = input('password>>: ') db[name] = {"password": password, "money": 0, "locked": 0} logger.info('登录成功') print('登录成功') current_user['user'] = name current_user['login_time'] = time.time() common.save_db(db) return func(*args, **kwargs) else: logger.info('用户名不存在,且拒绝注册')
def wrapper(*args,**kwargs): if current_user['user']: interval=time.time()-current_user['login_time'] if interval<current_user['timeout']: return func(*args,**kwargs) name=input('name>>:') db=common.conn_db() if db.get(name): # 已注册用户的登录流程 if db.get(name).get('locked'): logger.warning('该用户已被锁定') print('该用户已被锁定') else: logging_error_times=0 while True: if logging_error_times>=3: logger.warning('密码输入错误3次,该用户已被锁定') db[name]['locked']=1 common.save_db(db) break password=input('password>>:') if password==db.get(name).get('password'): logger.info('登录成功') print('登录成功') current_user['user']=name current_user['login_time']=time.time() return func(*args,**kwargs) else: logger.warning('密码错误') logging_error_times+=1 else: # 注册 is_register=input('是否注册? (Y/N) ') if is_register in ['Y','y']: password=input('password>>') db[name]={'password':password,'money':0,'locked':0} logger.info('登录成功') print('登录成功') current_user['user']=name current_user['login_time']=time.time() common.save_db(db) return func(*args,**kwargs) else: logger.info('用户不注册')
def buy(): prod_list = {1: ['洗衣机', 100], 2: ['手机', 20], 3: ['毛巾', 3]} name = input('name>>: ') db = common.conn_db() salary = db.get(name).get('money') goods = [] print("商品列表:\n编号 名称 价格") for list in prod_list: print('{:^2} {:^7}{:^4}'.format(list, prod_list[list][0], prod_list[list][1])) while (True): number = (input("请输入你要选择的商品编号(输入no可退出):")) if number == 'no': tag = False break number = int(number) if prod_list[number][1] < salary: salary = salary - prod_list[number][1] balance = salary goods.append(prod_list[number][0]) else: print("余额不足")
def change_res(name, price): dic = common.conn_db() dic[name]['money'] = price with open(BASE_DIR + '/db/db.json', 'w') as f: j = json.dump(dic, f)
def usr_res(name): dic = common.conn_db() return dic[name]['money']
# import sys,os # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # sys.path.append(BASE_DIR) import json import time from conf import settings from lib import common logger = common.get_logger(__name__) # 用户信息 dic = common.conn_db() # 商品信息 dic_com = common.conn_com() # 登录标志 login_flag = False # 先判断是否有此用户,若有,则登录;若无,则先注册再登录 def login_final(func): def tmp(): global dic name = input('Please input your name >> ') if name not in dic.keys(): print('Please register!') logger.info('Register') name = input('Please set login name >> ') password = input('Please set your password >> ') money = input('Please set your money >> ') dic[name] = {"password": password, "money": int(money)}