如何用Python计算月复合利息?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (234)
# This program takes the original principal,
# calculates the annual interest rate 
# calculates the number of times the interest is compounded
# calculates how many years the account will earn interest
# and lastly displays the ending principal


# Input the original principal.

original_principal = int(input( 'Enter the starting principal: ' ))

# Input the annual interest rate.

annual_interest = float(input( 'Enter the annual interest rate: ' ))

# Input times per year the interest is compounded.

compound = int(input( 'How many times per year is the interest compounded? ' ))

# Input number of years account will earn interest.

total_years = int(input( 'For how many years will the account earn interest? ' ))

# Calculate ending principle amount after earning annual
# interest for a specified amount of years.

ending_principal = original_principal * (1 + annual_interest / compound) ** \
               (compound * annual_interest)

#Display the ending principle amount.

print ( 'At the end of 2 years you will have $' , \
    format(ending_principal, ',.2f'))

我用1,000美元作为我的例子的主体,应该得到一个想要的结果_本金1,051.22美元。

然而,我离这个数字还很远,我猜我在计算的某个地方遗漏了一个括号。

提问于
用户回答回答于

这是固定代码,INTAST需要计算为十进制。

# This program takes the original principal,
# calculates the annual interest rate
# calculates the number of times the interest is compounded
# calculates how many years the account will earn interest
# and lastly displays the ending principal


# Input the original principal.

original_principal = int(input('Enter the starting principal: '))

# Input the annual interest rate.

annual_interest = float(input('Enter the annual interest rate (%): '))
annual_interest = annual_interest / 100

# Input times per year the interest is compounded.

compound = int(input('How many times per year is the interest compounded? '))

# Input number of years account will earn interest.

total_years = int(input('For how many years will the account earn interest? '))

# Calculate ending principle amount after earning annual
# interest for a specified amount of years.

ending_principal = (original_principal * (1 + annual_interest
                                      / compound) ** (compound * total_years)
                    )

# Display the ending principle amount.

print('At the end of ', total_years, 'years you will have $',
      format(ending_principal, '.2f'))

扫码关注云+社区

领取腾讯云代金券