简体   繁体   中英

Getting error: unsupported format character

My query is to write a function that calculates and computes the balance of a bank account with a given initial balance and interest rate, after a given number of years. Assume interest is compounded yearly.

Here is the code I have so far but I am getting the following error traceback

(most recent call last):
  File "C:\Users\Brandon\Desktop\Assignment 5\Assignment 5 question 4.py", line 46, in <module>
    Compount_Interest(B, I, N, T)
  File "C:\Users\Brandon\Desktop\Assignment 5\Assignment 5 question 4.py", line 40, in Compount_Interest
    print("The compound interest for %.InputT years is %.Cinterest" %Cinterest)
ValueError: unsupported format character 'I' (0x49) at index 28

and I have no clue why. Please help. Here is my code:

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()

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)

The error unsupported format character 'I' means that you are using the wrong (an unknown) conversion type for your string formatting.

By using the interpolation operator % (also known as string formatting operator ) it will give you access to a conversion type which is also expected to be named after setting the operator. As you can see in the documentation there are more optional characters you can include to refine the behavior/result of the operator but the last one (the character which represents the conversion type) is mandatory.

Your problem can be boiled down to this code which will give the same error:

>>> "%.InputT" % "foo"
ValueError: unsupported format character 'I' (0x49) at index 2

As you can see here (index 2 references to the I in the left string) the I is used as an identifier for the conversion type to be used. This is because the . is interpreted as optional precision of the minimum field width. And so the next character is the format character .

If you want to refer to a string you can simply use %s (or other format characters leading to a specific conversion type: int with %d and float with %f ) like this:

>>> "%s got $%d, now he has $%f total." % ("Peter", 12, 12.55)
'Peter got $12, now he has $12.550000 total.'

But it seems that you trying to name your placeholders. So you have to use parentheses for that and refer to it via a dictionary:

>>> "%(name)s got $%(dollars)d" % {"name": "Maria", "dollars": 50}
'Maria got $50'

For your code there is another obvious problem. You're naming InputT and Cinterest but only give a resolution to InputT . I think you want to do something like this:

print("The compound interest for %(InputT)s years is %(Cinterest)s" % {
    "InputT": InputT, "Cinterest": Cinterest})

# or without naming it explicitly
print("The compound interest for %s years is %s" % (InputT, Cinterest))

BTW: You're using old-style string formatting by using % -operator instead of new-style method .format() or since Python3.6 even f-strings like f"…" . This can make your live much simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM