2.python Simulated ant loan (original)

Source: Internet
Author: User

The ants borrowed

1. Implement

#!/usr/bin env python#-*-coding:utf-8-*-import datetimeimport timeimport redef Register (name, account, date, mounths): ':p Aram Name: Borrower's name:p Aram Date: Borrowing date:p Aram account: Borrowing amount:p Aram mounths: Number of months: return: "fi  Rstline = ' 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.W        Rite (Thridline) f1.write (fourthline) f1.write (lastline) if exist (name): Return True Else: return Falsedef Repay (name, account, time): "' Repayment:p Aram Name: User name:p Aram: Repayment amount: Return: Remaining repayment amount,             How much money should be paid "' F = ' green light ' result = [] with open (' Datebase.log ', ' R ', encoding= ' Utf-8 ') as F1:for line in F1: If Line.striP () = = ' username:%s '% name:f = ' red light ' continue if f = = ' red light ': if Li                    Ne.strip (). StartsWith (' Username '): F = ' green light ' break else: If Line.strip (): Result.append (Line.strip ()) F = ' green light ' Replace1 = Re.search (R ' \d+ ', resu Lt[0]). Group () Replace2 = str (int (REPLACE1)-int (account) result[0] = Result[0].replace (Replace1, Replace2) rep Lacea, replaceb, REPLACEC = Re.search (R ' (\d+)-(\d+)-(\d+) ', result[1]). Groups () Replace2 = Replacea + '-' + str (INT (rep LACEB) + 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 ', Enco   ding= ' Utf-8 ') as NEW:     For line in Old:if line.strip () = = ' username:%s '% name:f = ' red light ' new.write (line) for new_line in Result:new.write (' * 8 + new_line + ' \ n ') Conti Nue if f = = ' green light ': New.write (line) else:if Line.strip (). StartsWith (' Ba Ckend '): F = ' green light ' 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 Cha  Xun (name): ":p Aram Name: User name: return: Monthly repayment condition ' F = ' green light ' result = [] with open (' Datebase.log ', ' R ', encoding= ' Utf-8 ') as F1:for line in F1:if line.strip () = = ' username:%s '% name:f = ' Red Lamp ' Result.append (Line.strip ()) Continue if f = = ' red light ': if Line.stri P (). StartsWith (' Username'): F = ' green light ' break Else:if Line.strip (): Result.append (Line.strip ()) return resultdef exist (name): Flage = ' cannot find ' with open (' Datebase.log ',  ' R ', encoding= ' Utf-8 ') as F:for line in F:if line.startswith (' Username: ' + name): Flage = ' found ' break if flage = = ' found ': Return True else:return falsedef Lix  I (list): # [' Username:wuwen ', ' money:1000 ', ' date:2017-8-10 ', ' times:10 ', ' Interest ': 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.sear CH (R ' \d+ ', list[1]). Group ()) for I in range (1, int (times) + 1): if int (mounths) < 12:mounths = St             R (Int (mounths) + 1) date = '%s-%2s-%2s '% (years, mounths, days) Money_oringin = Moneys/int (Times) Money_intereSt = Moneys * 0.0004 * print (' \033[36;1m%2s period: 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) mounth s = str (int (mounths)-one) date = '%s-%2s-%2s '% (years, mounths, days) Money_oringin = Moneys/int (times) money_interest = Moneys * 0.0004 * 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 = mo Neys-money_oringin times = str (int (times)-1) continueif __name__ = = ' __main__ ': # \033[31;1m \033[0m 1 is Red 2 for green 3 for yellow 4 for dark blue 5 grape red 6 light blue 7 light gray 8 black while true:options = input (' \033[31;1m Please enter option, 1: Borrow money 2: Pay back $3: query Enter Yo ur option:\033[0m ') if options = = ' 1 ': While true:name = input (' \033[32;1m Please enter username: \033[0m ' if exist (name): result = Chaxun (name) remain = Result[1].split (': ')                        ) [1] if int (remain) > 0:print (' \033[33;1m please pay off the balance and try to borrow money again: \033[0m ')                    Continue else:account = input ("\033[33;1m input you want to borrow to get amount: \033[0m") Mounths = input ("\033[33;1m Enter the number of months you are staging, 6 or 12:\033[0m ") date = str (Datetime.date.today ()) If register (name, acc Ount, date, mounths): Print (' \033[34;1m borrows the success, the daily rate is four out of 10,000, please repay 5 days before the next month!! \033[0m ') break elif options = = ' 2 ': while true:name = input (' \03 3[32;1m Please enter user name: \033[0m ') if exist (name): result = Chaxun (name) print (' \033[33;1m%s\033[0m '% result) lixi (result) times = input (' \033[33;1m Enter the number you want: \033[0                    M ') account = input (' \033[33;1m Please enter the correct principal: \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 user does not exist!! \033[0m ') Continue elif options = = ' 3 ': while TruE:name = input (' \033[32;1m Please enter the user name to query: \033[0m ') if exist (name): result = Chaxun (name) print (' \033[33;1m%s\033[0m '% result) Lixi (result) b Reak Else:print ("No This user") Continue print (' \033[34;1m3s back home \03 3[0m ') Time.sleep (3)

2. Effect display

  1.1: Borrowing money

  

  2.1: Storage format

  

  3.1: Pay back

  

  3.2: After modification

  

  4.1: Query

  

2.python Simulated ant loan (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.