python 複利計算

2021-10-02 03:08:56 字數 2460 閱讀 3471

寫乙個理財計算器,實現將每日/月/年的利息進行復投進行計算

fv(pv,i,n) = pv*(1+i)**n 

fuli.py

# coding: utf-8

# 寫乙個理財計算器,實現將每日/月/年的利息進行復投進行計算

money = float(input('請輸入您打算用來投資的本金:pv= '))

year = int(input('請輸入投資期限(單位:年):n= '))

rate = float(input('請輸入投資年化收益率:i= '))

mm = int(input('''0.每日 1.每月 3.每3月 6.每6月 12.每年 請選擇利息復投方式: '''))

def day_return(money,year,rate=0.06):

'''方案:每日利息加入本金復投'''

for y in range(year):

for day in range(365):

money = money*rate/365 + money

print('第%d年結束時,本利合計:%.2f' % (y+1,money))

def month_return(money,year,mm,rate=0.06):

'''方案:每月利息加入本金復投'''

for y in range(year):

cs = 12//mm

for month in range(cs):

money = money*rate/cs + money

print('第%d年結束時,本利合計:%.2f' % (y+1,money))

def year_return(money,year,rate=0.06):

'''方案:每年利息加入本金復投'''

for y in range(year):

money = money*rate + money

print('第%d年結束時,本利合計:%.2f' % (y+1,money))

if mm == 0:

day_return(money,year,rate)

elif mm in (1,2,3,4,6):

month_return(money,year,mm,rate)

elif mm == 12:

year_return(money,year,rate)

else:

print('mm 輸入有誤!')

例題1. 某公司於2023年初向銀行存入5萬元資金,年利率為 6%,按半年複利計息,則 2023年到期時可以得到的本利和為:

python fuli.py

請輸入您打算用來投資的本金:pv= 50000

請輸入投資期限(單位:年):n= 10

請輸入投資年化收益率:i= 0.06

0.每日 1.每月 3.每3月 6.每6月 12.每年 請選擇利息復投方式: 6

第1年結束時,本利合計:53045.00

第2年結束時,本利合計:56275.44

第3年結束時,本利合計:59702.61

第4年結束時,本利合計:63338.50

第5年結束時,本利合計:67195.82

第6年結束時,本利合計:71288.04

第7年結束時,本利合計:75629.49

第8年結束時,本利合計:80235.32

第9年結束時,本利合計:85121.65

第10年結束時,本利合計:90305.56

例題2. f先生將一筆10萬元的資金投入在年收益率為 6%的工程專案中,試估算,大約經過(?)年,本利和可以達到20萬元。

returns.py

# coding: utf-8

#手動輸入回報率

rate = float(input("年收益率: "))

#設定初始金額,預設10w

money = 100000

fv = 200000

count = 1

while count <= 100:

returns = money*(1 + rate)**count

if returns > fv :

print("第"+str(count)+"年: "+str(int(returns))+"元")

break

count += 1

python returns.py

年收益率: 0.06

第12年: 201219元

72法則:翻番時間=72/利率  例如:6% 翻番需要 72/6=12年

雅各布·伯努利在研究複利時發現的自然常數:

(1+(1/x))^x的極限就約等於e:=2.718281828...

(1+(1/365))**365 = 2.7145674820219727

納斯達克在網際網路世紀大癲狂時曾經花了一年半時間從約1357點暴漲至5132點,漲了278%。

03 13 複利計算

include include write double i1,int p1,int n1 printf 請輸入本金 scanf d p1 printf 請輸入年利率 scanf lf i1 while i1 100 printf 請重新輸入年利率 scanf lf i1 printf 請輸入年限 ...

複利計算3 0

程式構造思路 第一點 在求複利計算中達到終值所需年份是利用窮舉法計算1 100年間符合的時間,最終求出存款所需時間。第二點 在求複利計算中利用複利公式的逆推,求出年利率的公式。include include include void year void time if flat 0 printf 在...

複利計算4 0

1 include2 include3 include4 void view 524 25void danli 2640 41 42void year 4361 62 years i 1 63 printf 所需年數為 d n n n years 64 6566 void nianlilv 6781...