Home > Software design >  New to Python, trying to fix TypeError: can only concatenate str (not "int") to str in my
New to Python, trying to fix TypeError: can only concatenate str (not "int") to str in my

Time:01-31

I am trying to create a simple savings calculator, my problem im running into is when I added user input I run into TypeError: can only concatenate str (not "int") to str

def save():
    # Set the target savings amount and the number of months
    target_savings = input("Please input your savings goal.")
    print("")
    months = input("Please input how many months you want to take to reach this goal.")
    print("")

    # Initialize the current savings amount
    current_savings = 0

    # Set monthly income
    monthly_income = input("Please input your monthly income")
    print("")

    # Loop through each month
    for month in range(1, months   1):
        # Get the monthly expenses
        expenses = float(input("Enter expenses for month {month}: $"))
        # Calculate the monthly savings
        monthly_savings = monthly_income - expenses
        # Add the monthly savings to the current savings
        current_savings  = monthly_savings
        # Check if the current savings is enough to reach the target savings
        if current_savings >= target_savings:
            print(f"Congratulations! You reached your savings goal of ${target_savings} in month {month}.")
            break
        else:
            print(f"Month {month}: Savings = ${current_savings}, Goal = ${target_savings}")

# Call the function to start the savings tracker
save()

Error

for month in range(1, months   1):
TypeError: can only concatenate str (not "int") to str
  • I have tried online research from similar problems

CodePudding user response:

Try write

target_savings = int(input("Please input your savings goal."))
print("")
months = int(input("Please input how many months you want to take to reach this goal."))
print("")
...
monthly_income = float(input("Please input your monthly income"))

CodePudding user response:

Just change your input functions to integers:

def save():
    # Set the target savings amount and the number of months
    target_savings = int(input("Please input your savings goal."))
    print("")
    months = int(input("Please input how many months you want to take to reach this goal."))
    print("")

    # Initialize the current savings amount
    current_savings = 0

    # Set monthly income
    monthly_income = int(input("Please input your monthly income"))
    print("")

    # Loop through each month
    for month in range(1, months   1):
        # Get the monthly expenses
        expenses = int(input("Enter expenses for month {month}: $"))
        # Calculate the monthly savings
        monthly_savings = monthly_income - expenses
        # Add the monthly savings to the current savings
        current_savings  = monthly_savings
        # Check if the current savings is enough to reach the target savings
        if current_savings >= target_savings:
            print(f"Congratulations! You reached your savings goal of ${target_savings} in month {month}.")
            break
        else:
            print(f"Month {month}: Savings = ${current_savings}, Goal = ${target_savings}")

# Call the function to start the savings tracker
save()
  • Related