## 如何用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 个回答

```# 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'))```