2.python模拟蚂蚁借呗(原创)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2.python模拟蚂蚁借呗(原创)相关的知识,希望对你有一定的参考价值。

蚂蚁借呗

1.实现

#!/usr/bin env python
# -*- coding:utf-8 -*-
import datetime
import time
import re


def register(name, account, date, mounths):
    ‘‘‘
    :param name: 借款人姓名
    :param date: 借款日期
    :param account: 借款金额
    :param mounths: 分期月数
    :return:
    ‘‘‘
    firstLine = ‘Username:‘ + name + ‘\\n‘
    secondLine = ‘ ‘ * 8 + ‘Money:‘ + account + ‘\\n‘
    thridLine = ‘ ‘ * 8 + ‘Date:‘ + date + ‘\\n‘
    fourthLine = ‘ ‘ * 8 + ‘Times:‘ + mounths + ‘\\n‘
    lastLine = ‘ ‘ * 8 + ‘Interest(day):0.0004‘ + ‘\\n‘
    with open(‘datebase.log‘, ‘a‘, encoding=‘utf-8‘) as f1:
        f1.write(firstLine)
        f1.write(secondLine)
        f1.write(thridLine)
        f1.write(fourthLine)
        f1.write(lastLine)

    if exist(name):
        return True
    else:
        return False


def repay(name, account, time):
    ‘‘‘
    还款
    :param name: 用户名
    :param account: 还款金额
    :return: 剩余还款金额,应还多少金额
    ‘‘‘
    f = ‘绿灯‘
    result = []
    with open(‘datebase.log‘, ‘r‘, encoding=‘utf-8‘) as f1:
        for line in f1:
            if line.strip() == ‘Username:%s‘ % name:
                f = ‘红灯‘
                continue
            if f == ‘红灯‘:
                if line.strip().startswith(‘Username‘):
                    f = ‘绿灯‘
                    break
                else:
                    if line.strip():
                        result.append(line.strip())
    f = ‘绿灯‘

    replace1 = re.search(r‘\\d+‘, result[0]).group()
    replace2 = str(int(replace1) - int(account))
    result[0] = result[0].replace(replace1, replace2)

    replaceA, replaceB, replaceC = re.search(r‘(\\d+)-(\\d+)-(\\d+)‘, result[1]).groups()
    replace2 = replaceA + ‘-‘ + str(int(replaceB) + int(time)) + ‘-‘ + replaceC
    result[1] = result[1].replace(replaceA + ‘-‘ + replaceB + ‘-‘ + replaceC, replace2)

    replace1 = re.search(r‘\\d+‘, result[2]).group()
    replace2 = str(int(replace1) - int(time))
    result[2] = result[2].replace(replace1, replace2)

    with open(‘datebase.log‘, ‘r‘, encoding=‘utf-8‘) as old, open(‘NewDatebase.log‘, ‘w‘, encoding=‘utf-8‘) as new:
        for line in old:
            if line.strip() == ‘Username:%s‘ % name:
                f = ‘红灯‘
                new.write(line)
                for new_line in result:
                    new.write(‘ ‘ * 8 + new_line + ‘\\n‘)
                continue
            if f == ‘绿灯‘:
                new.write(line)
            else:
                if line.strip().startswith(‘backend‘):
                    f = ‘绿灯‘
                    new.write(line)

    with open(‘NewDatebase.log‘, ‘r‘, encoding=‘utf-8‘) as old, open(‘datebase.log‘, ‘w‘, encoding=‘utf-8‘) as new:
        for line in old:
            new.write(line)


def chaxun(name):
    ‘‘‘
    :param name:用户名
    :return:每月还款情况
    ‘‘‘
    f = ‘绿灯‘
    result = []
    with open(‘datebase.log‘, ‘r‘, encoding=‘utf-8‘) as f1:
        for line in f1:
            if line.strip() == ‘Username:%s‘ % name:
                f = ‘红灯‘
                result.append(line.strip())
                continue
            if f == ‘红灯‘:
                if line.strip().startswith(‘Username‘):
                    f = ‘绿灯‘
                    break
                else:
                    if line.strip():
                        result.append(line.strip())
    return result


def exist(name):
    flage = ‘找不到‘
    with open(‘datebase.log‘, ‘r‘, encoding=‘utf-8‘) as f:
        for line in f:
            if line.startswith(‘Username:‘ + name):
                flage = ‘找到了‘
                break
        if flage == ‘找到了‘:
            return True
        else:
            return False


def lixi(list):  # [‘Username:wuwen‘, ‘Money:1000‘, ‘Date:2017-8-10‘, ‘Times:10‘, ‘Interest(day):0.0004‘]

    years, mounths, days = re.search(r‘(\\w+)-(\\w+)-(\\w+)‘, list[2]).groups()
    times = re.search(r‘\\d+‘, list[3]).group()
    moneys = int(re.search(r‘\\d+‘, list[1]).group())
    for i in range(1, int(times) + 1):

        if int(mounths) < 12:
            mounths = str(int(mounths) + 1)
            date = ‘%s-%2s-%2s‘ % (years, mounths, days)
            money_oringin = moneys / int(times)
            money_interest = moneys * 0.0004 * 30
            print(‘\\033[36;1m第%2s期:Date:%s Moneys + interest: %5s + %5s = %5.2f\\033[0m‘ % (i,
                                                                                           date, money_oringin,
                                                                                           money_interest,
                                                                                           money_interest + money_oringin))
            moneys = moneys - money_oringin
            times = str(int(times) - 1)
        else:
            years = str(int(years) + 1)
            mounths = str(int(mounths) - 11)
            date = ‘%s-%2s-%2s‘ % (years, mounths, days)
            money_oringin = moneys / int(times)
            money_interest = moneys * 0.0004 * 30
            print(‘\\033[36;1m第%2s期:Date:%s Moneys + interest: %5s + %5s = %5.2f\\033[0m‘ % (i,
                                                                                           date, money_oringin,
                                                                                           money_interest,
                                                                                           money_interest + money_oringin))
            moneys = moneys - money_oringin
            times = str(int(times) - 1)
            continue


if __name__ == ‘__main__‘:
    # \\033[31;1m \\033[0m  1为红色 2为绿 3为黄 4为深蓝 5葡萄红 6浅蓝 7浅灰 8黑色
    while True:
        options = input(‘\\033[31;1m请输入选项,1:借钱 2:还钱 3:查询 Enter Your Option:\\033[0m‘)
        if options == ‘1‘:
            while True:
                name = input(‘\\033[32;1m请输入用户名:\\033[0m‘)
                if exist(name):
                    result = chaxun(name)
                    remain = result[1].split(‘:‘)[1]
                    if int(remain) > 0:
                        print(‘\\033[33;1m请还清余额再尝试借钱:\\033[0m‘)
                        continue
                else:
                    account = input("\\033[33;1m输入你要借取得金额:\\033[0m")
                    mounths = input("\\033[33;1m输入你分期的月数,6或12:\\033[0m")
                    date = str(datetime.date.today())
                    if register(name, account, date, mounths):
                        print(‘\\033[34;1m借取成功,日利率为万分之四,请在下个月今天5天之前还款!!\\033[0m‘)
                        break

        elif options == ‘2‘:
            while True:
                name = input(‘\\033[32;1m请输入用户名:\\033[0m‘)
                if exist(name):
                    result = chaxun(name)
                    print(‘\\033[33;1m%s\\033[0m‘ % result)
                    lixi(result)
                    times = input(‘\\033[33;1m输入你要还的次数:\\033[0m‘)
                    account = input(‘\\033[33;1m请输入正确的本金:\\033[0m‘)
                    repay(name, account, times)
                    result = chaxun(name)
                    print(‘\\033[33;1m%s\\033[0m‘ % result)
                    lixi(result)
                    break
                else:
                    print(‘\\033[33;1m用户不存在!!\\033[0m‘)
                    continue

        elif options == ‘3‘:
            while True:
                name = input(‘\\033[32;1m请输入要查询的用户名:\\033[0m‘)
                if exist(name):
                    result = chaxun(name)
                    print(‘\\033[33;1m%s\\033[0m‘ % result)
                    lixi(result)
                    break
                else:
                    print("没有此用户")
                    continue
        print(‘\\033[34;1m3s后返回首页\\033[0m‘)
        time.sleep(3)

 

2.效果展示

  1.1:借钱

  技术分享

  2.1:存放格式

 

  技术分享

 

  3.1:还钱

  技术分享

  3.2:修改之后

  技术分享

  4.1:查询

  技术分享

 

以上是关于2.python模拟蚂蚁借呗(原创)的主要内容,如果未能解决你的问题,请参考以下文章

《蚂蚁短视频》卖掉秘豆流程介绍

BT蚂蚁使用方法

从刷屏的“蚂蚁呀嘿”到“蚂呀完了”,谁在挖坑谁在跳?

蚂蚁金融推迟上市:互联网金融是否要遭遇滑铁卢

支付宝的蚂蚁积分用途很多,别不当回事儿,用得好能省很多钱哦!

每月博客-20170604