Skip to content
Advertisement

For loop monthly budget program creating error

I am running this for loop code and it is creating an error, I cannot find out the problem with it

JavaScript

Any ideas on why I am getting this error? I have tried to figure it out on my own but I dont know why it is wrong

Advertisement

Answer

Your problem is that the for-loop variable month is an integer, so you can’t concatenate it to a string without converting it first. The easiest way to fix that is to use a format-string instead.

For example, this line: AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))

Should be changed to: float(input(f"Enter amount budgeted for month {month}:"))

(If you’re on an older version of Python you would write float(input("Enter amount budgeted for month {month}:".format(month=month))) instead.)

If you’re hard-set on using concatenation instead, the trick is to convert month to a string first using str(month).

Hope that helps!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement