首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Python计算每月复利

使用Python计算每月复利
EN

Stack Overflow用户
提问于 2018-06-17 09:46:54
回答 1查看 1.8K关注 0票数 1
代码语言:javascript
复制
# 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作为我的original_principal (仅作为示例),并且应该得到$1,051.22的理想ending_principal。

然而,我与这个数字相去甚远。我猜我在计算中遗漏了一个括号。如果我没有正确地格式化这段代码,我提前道歉,这是我第二次使用堆栈溢出,还在学习。

EN

回答 1

Stack Overflow用户

发布于 2018-06-17 10:12:54

这是固定的代码。利息需要以小数计算。

代码语言:javascript
复制
# 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'))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50893048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档