python小程序--计算现值工具

 1 #! /usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author Ian Ying
 4 # 
 5 
 6

import sys, math
import argparse


def main(type, years, money, rate):
if type == "future_each":
rate_t = (rate / 100) + 1
pow_v = math.pow(rate_t, years)
total = (1 - pow_v)/(1 - rate_t)
total_v = money * total
pow_v_p = math.pow(rate_t, years - 1)
total_v_p = total_v/ pow_v_p
print("In the %s year, Future money is %s" % (years, total_v))
print("Ex: years =2 . You cash is %s + (%s * %s) = %s" % (money, money, rate_t, total_v))
print("%s Present worth is %s" % (total_v, total_v_p))

elif type == "present_each":
rate_t = (rate / 100) + 1
pow_v = math.pow(rate_t, years)
total = ((pow_v - 1) * rate_t)/((rate_t - 1) * pow_v)
total_v = money * total
print("In the %s year, Present money is %s" % (years, total_v))
print("Ex: years =%s . You cash is %s + (%s / %s) = %s" % (years, money, money, rate_t, total_v))
elif type == "present_one":
rate_t = (rate / 100) + 1
pow_v = math.pow(rate_t, years)
total = pow_v
total_v = money / total
print("%s years later, Present money is %s" % (years, total_v))
elif type == "future_one":
rate_t = (rate / 100) + 1
pow_v = math.pow(rate_t, years)
total = pow_v
total_v = money * total
print("In %s year later , Future money is %s" % (years, total_v))
return 1


if __name__ == '__main__':
parser = argparse.ArgumentParser(description="used for computing money future value or present value.")
parser.add_argument("-t", "--type", choices=["future_each", "present_each", "future_one", "present_one"], help="choose the type for computing.")
parser.add_argument("-y", "--years", type=float, help="input how many years.")
parser.add_argument("-m", "--money", type=float, help="input how much money for one year.")
parser.add_argument("-r", "--rate", type=float, help="input the rate for the every year. unit is %")
args = parser.parse_args()
main(args.type, args.years, args.money, args.rate)
print("Successful done")
 

使用范例:

>money_computer.py -t future_each -y 30 -m 1460 -r 5
In the 30.0 year, Future money is 97000.7173544
Ex: years =2 . You cash is 1460.0 + (1460.0 * 1.05) = 97000.7173544
97000.7173544 Present worth is 23565.9674242
Successful done

money_computer.py -t present_one -y 40 -m 500000 -r 5
40.0 years later, Present money is 71022.8411501
Successful done

原文地址:https://www.cnblogs.com/Ian-learning/p/11581310.html