# 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。
然而,我远不及那个数字。 我的猜测是我在计算中的某个地方错过了一个括号。 如果我没有正确格式化这段代码,我会事先道歉,这是我第二次使用堆栈溢出,仍在学习。
答案 0 :(得分:0)
这是固定代码。兴趣需要以小数计算。
# 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'))