CWoolford
CWoolford

Reputation: 15

Not sure why I'm getting "TypeError: can only concatenate str (not "int") to str"

I had working code, but it gave me the same Invest_amount every year (did not add the previous year's interest), so I created a Starting_balance to try and fix this.

However, now it gives me this error message:

TypeError: can only concatenate str (not "int") to str. But I put str in front of the value?

My code:

InvestAmount = int(input("Enter the intial investment amount: "))
Years = int(input("Enter the number of years to invest: "))
Rate = float(input("Enter the intrest rate (as %): "))
TotalInterestEarned = 0
for i in range(Years):
     InterestEarned = round(InvestAmount*(Rate/100),2)
     EndingBal = round(InvestAmount+InterestEarned , 2)
     Starting_balance = (InvestAmount + InterestEarned)
     print("Starting Balance: for year " + (i+1)+"$"+str(Starting_balance))    
     print("Ending balance: for year" +(i+1)+"$"+str(EndingBal))
     print("Total interest earned: for year" +(i+1)+"$"+str(InterestEarned))

Upvotes: 1

Views: 5402

Answers (6)

Grismar
Grismar

Reputation: 31396

"abc" + (i+1) is adding an integer to a string. Consider using an f-string like this:

print(f"Starting Balance: for year {i+1} ${Starting_balance}")

It's more readable and performs the needed casting for you.

Upvotes: 3

Balaji
Balaji

Reputation: 1087

For completeness sake another approach would be to use , and let the print function do its thing.

The print function takes a single object or multiple objects seperated by , and will be convert to a string and be printed on the screen.

InvestAmount = int(input("Enter the intial investment amount: "))
Years = int(input("Enter the number of years to invest: "))
Rate = float(input("Enter the intrest rate (as %): "))


TotalInterestEarned = 0
for i in range(Years):
    InterestEarned = round(InvestAmount * ( Rate / 100),2)
    EndingBal = round(InvestAmount + InterestEarned , 2)
    Starting_balance = (InvestAmount + InterestEarned)
      
    print("Starting Balance: for year ", (i + 1), "$", Starting_balance)    
    print("Ending balance: for year", (i + 1), "$", EndingBal)
    print("Total Interest Earned: for year", (i + 1), "$", InterestEarned)

This is not the best approach, The other answers are much better.

Upvotes: 0

Kevin Jiang
Kevin Jiang

Reputation: 313

Everybody has pretty much gone over the solution but I hopefully can add a little more context for those unfamiliar with types. So when you define i, it is an int. On the other hand, the string you're trying to put it in is "Starting Balance:..." meaning it is a string. With integers 5 + 5 = 10. Adding integers is standard. Similarly, python is clever enough to understand "con" + "cat" = "concat" as essentially you're tacking on the end of a string.

Now what happens when you do "con" + 5 = ERROR because python doesn't understand what adding an integer to a string means. Does it mean you should turn "con" into an integer representation and add 5 that way or does it mean to tack the number five so it becomes "con5"? (Note some language allow this addition and it sometimes can get weird. Python is strongly typed so we don't have to consider that possibility.)

In other words, we have to make that distinction otherwise we're mismatching types. In all the solutions, what's happening is we're turing i+1 into some string, either by enacting str() or f-strings.

Hope this clarifies!

Upvotes: 1

PW1990
PW1990

Reputation: 479

You just needed to prepend str to (i+1) like so:

print("Starting Balance: for year " + str((i + 1)) + " $" + str(Starting_balance))
print("Ending balance: for year " + str((i + 1)) + " $" + str(EndingBal))
print("Total Interest Earned: for year " + str((i + 1)) + " $" + str(InterestEarned))

Upvotes: 0

Shreyas Prakash
Shreyas Prakash

Reputation: 614

You can only concatenate two strings, you are trying to concatenate int and str


InvestAmount = int(input("Enter the intial investment amount: "))
Years = int(input("Enter the number of years to invest: "))
Rate = float(input("Enter the intrest rate (as %): "))



TotalInterestEarned = 0
for i in range(Years):
      InterestEarned = round(InvestAmount*(Rate/100),2)
      EndingBal = round(InvestAmount+InterestEarned , 2)
      Starting_balance = (InvestAmount + InterestEarned)
      
      print("Starting Balance: for year " + str((i+1))+"$"+str(Starting_balance))  # change int to str as well str(i+1)  
      print("Ending balance: for year" +str((i+1))+"$"+str(EndingBal)) # same here 
      print("Total Interest Earned: for year" +str((i+1))+"$"+str(InterestEarned)) # same here

Upvotes: 1

user15256253
user15256253

Reputation:

You can do like this:

print(f"Starting Balance: for year {i+1} $ {Starting_balance}") 
# and other code... 

Upvotes: 0

Related Questions