iT邦幫忙

0

Python解題 計算?

1.小偉買一12年基金,每年年初買入該基金,每年皆繳10,000,且每年以升值2%去計算,請寫一個程式,請問直到第十二年年末,每年基金價值為多少,將12年期基金於12年年末的價值列印出來?
第一年:
....
第十一年:10,404
第十二年:10,202

2.小偉想要知道他要花多少年(從第一年開始計算)這12年基金總價值才會超過1,000,000元?列印出在第幾年時,小明的基金總價值為多少?以及每年的基金價值為多少?

目前卡在這邊,雖然知道說要加個迴圈繼續運算,跟判斷式當價值超過一百萬把年份印出來,但還是無法成功,想知道問題出在哪?謝謝各位了

import math price=10000 sum=0 for i in range(1,13): a=price*(1.02**i) sum=sum+a print(math.floor(sum*1000)/1000) while sum <= 1000000: year += 1 print(year)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
Alien
iT邦新手 4 級 ‧ 2021-01-11 00:48:34

我覺得現金流要先搞懂

fund_value = 0

for i in range(1, 13):
    fund_value += 10000  
    fund_value = fund_value * 1.02
    print('fund_value', i, int(fund_value))

fund_value = 0
year = 0

while fund_value <= 1000000:
    fund_value += 10000 
    fund_value = fund_value * 1.02
    year += 1

print(year)
0
japhenchen
iT邦超人 1 級 ‧ 2021-01-11 16:40:52

俺不講理論,直接實作

v = 10000 # 每期本金
i = 0.02 #利息
total = 0
x=1
while total<=1000000:
    total += ((v if x <= 12 else 0) + total* i ) # 第13期開始就不投入本金
    if x==12 :
        print("第12期時,本利和{0}元".format(round(total)))

    if total>1000000:
        break
    x+=1
print("第{0}期時,本利和{1}元".format(x,round(total)))

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-01-12 09:40:22

https://ithelp.ithome.com.tw/upload/images/20210112/20001787WYUqnw3AGX.png

import math

price=10000
sum=0
fund=0
i=1
while fund < 1000000:
    a=price*(1.02**i)
    sum=sum+a
    fund=math.floor(sum*1000)/1000
    print(i, fund)
    i=i+1

我要發表回答

立即登入回答