使用Python函数计算复利

2024-06-16 11:40:44 发布

您现在位置:Python中文网/ 问答频道 /正文

写一个函数,在给定的年数后,用给定的初始余额和利率计算银行帐户的余额。假设利息每年复利。在

出现错误“ValueError:索引28处不支持格式字符'I'(0x49)”

这是我目前为止的代码。在

def BankBalance():
    InputB = 1000
    return InputB
    print("Your initial balance is $1000")

def Interest():
    InputI = 0.05
    return InputI
    print("The rate of interest is 5%")

def CountNumber():
    InputN = float(input("Please enter the number of times per year you would like your interest to be compounded: "))
    return InputN

def Time():
    InputT = float(input("Please enter the number of years you need to compund interest for:"))
    return InputT

def Compount_Interest(InputB, InputI, InputT, InputN):
    Cinterest = (InputB*(1+(InputI % InputN))**(InputN * InputT))
    print("The compound interest for %.InputT years is %.Cinterest" %Cinterest)

B = BankBalance()
I = Interest()
N = CountNumber()
T = Time()
Compount_Interest(B, I, N, T)

Tags: ofthereturnisdef余额printinterest
3条回答

您正在尝试将变量用作函数。 试试这个:

Cinterest = (InputB * (1+(InputI % InputN))**(InputN * InputT))

Python和大多数其他编程语言都不认为两个相邻的数学表达式之间没有运算符意味着乘法。在InputB和表达式其余部分之间缺少一个乘法运算符(*):

Cinterest = (InputB * (1+(InputI % InputN))**(InputN * InputT))
# Here       -^

下面是你应该怎么做。在

def main():
# Getting input for Balance
    balance = float(input("Balance: $ "))
# Getting input for Interest Rate
    intRate = float(input("Interest Rate (%) : "))
# Getting input for Number of Years
    years = int(input("Years: "))
    newBalance = calcBalance(balance, intRate, years)

    print ("New baance:  $%.2f"  %(newBalance))
def calcBalance(bal, int, yrs):
    newBal = bal
    for i in range(yrs):
        newBal = newBal + newBal * int/100
    return newBal

# Program run
main()

相关问题 更多 >