Last active
January 10, 2021 07:32
-
-
Save Linusp/e966409361605dcfff9d47015373ed08 to your computer and use it in GitHub Desktop.
计算达到资产目标需要多少年
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
@click.command() | |
@click.option("--age", type=int, default=24, help="你当前的年龄") | |
@click.option("--asset", type=float, default=0, help="上一年年末总资产") | |
@click.option("--salary", type=float, default=200000, help="预计今年年工作收入") | |
@click.option("--salary-rate", type=float, default=0.1, help="预计工作收入年增长率") | |
@click.option("--invest", type=float, default=10000, help="预计今年投资收入") | |
@click.option("--invest-rate", type=float, default=0.1, help="预计投资收入年增长率") | |
@click.option("--expense", type=float, default=90000, help="预计今年支出") | |
@click.option("--expense-rate", type=float, default=0.05, help="预计年支出增长率") | |
@click.option("--target", type=float, default=1000000.0, help="资产目标") | |
def main(age, asset, salary, salary_rate, invest, invest_rate, expense, expense_rate, target): | |
def cal_asset(n): | |
return ( | |
asset + | |
salary * ((1 + salary_rate) ** n - 1) / salary_rate + | |
invest * ((1 + invest_rate) ** n - 1) / invest_rate - | |
expense * ((1 + expense_rate) ** n - 1) / expense_rate | |
) | |
idx = 1 | |
print(f"你现在 {age} 岁,拥有资产 {asset} 元") | |
while True: | |
new_asset = cal_asset(idx) | |
if new_asset >= target: | |
print(f"第 {idx} 年,在你 {age + idx} 岁时,你存到了 {new_asset} 元,达到了目标!") | |
break | |
print(f"第 {idx} 年,你存到了 {new_asset} 元,未达到目标") | |
idx += 1 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment