Exemple #1
0
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\' ')
Exemple #2
0
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('请输入【商品名称】【商品数量】')
Exemple #3
0
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))
Exemple #4
0
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)
Exemple #5
0
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('你没有足够的钱')
Exemple #6
0
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('账户余额不足')
Exemple #7
0
    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('用户名不存在,且拒绝注册')
Exemple #8
0
 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('用户不注册')