Python Forum

Full Version: Resolving unsupported operand type +=: 'int' and 'str'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Very new to programming here and this is a homework question.

The output I'm trying to generate looks as follows:

ENTER ITEMS (ENTER 0 TO END)
Cost of item: 152.50
Cost of item: 59.80
Cost of item: 0
Total: 212.3
Sales tax: 12.74
Total after tax: 225.04
Again? (y/n): n
Thanks, bye!

The code I've created so far is as follows:

#!/usr/bin/env python3


def get_cost():
    total_cost = 0
    while True:
        print("ENTER ITEMS (ENTER 0 TO END)")
        cost = str(input("Cost of item: "))
        if cost == 0:
            print_cost(total_cost)
            break    
        else:
          total_cost += cost
            
def print_cost(total_cost):
    print("Total: " + str(total_cost))
    print("Sales tax: " + str(total_cost * .06))
    print("Total after tax: " + str(total_cost + (total_cost * .06)))
    print()

def main():
    print("Sales Tax Calculator")
    print()
    choice = "y"
    while choice.lower() == "y":
        get_cost()

        # see if the user wants to continue
        choice = input("Again? (y/n): ")
        print()

    print("Thanks, bye!")        
    
if __name__ == "__main__":
        main()

I get that there is an error in line 13 but I don't understand where it is or how to fix it.

The error code is
File "c:\Users\Student\Downloads\Project 4_401.py", line 35, in <module>
  main()
File "c:\Users\Student\Downloads\Project 4_401.py", line 26, in main
  get_cost()
File "c:\Users\Student\Downloads\Project 4_401.py", line 13, in get_cost
  total_cost += cost

builtins.TypeError: unsupported operand type(s) for +=: 'int' and 'str'
The input function returns a string on line 8. You make that a string again with str(). But total_cost is an integer, because you set it to 0 on line 5. You can't add a string to an integer, so you get the error. You int() on line 8, rather than str(), so that cost is an integer.
In order to get the output I'm looking for I need it to be able to show decimals. I'm trying to make total_cost all string so that I can round it to two decimals.
Then you need to convert total_cost back to a string after it is fully calculated. You cannot do mathematical calculations with strings, you need ints or floats.

Have you covered the format method of strings, or f-strings, in your class? Those would be the preferred method for rounding to two decimals. Doing it manually after converting to a string is overly complicated, and rule #1 is keep it simple.
Thank you for the assistance! Here is the correct code

#!/usr/bin/env python3


def get_cost():
    total_cost = 0
    print("ENTER ITEMS (ENTER 0 TO END)")
    while True:
        cost = float(input("Cost of item: "))
        if cost == 0:
            print_cost (total_cost)
            break    
        else:
            total_cost += cost
            
def print_cost(total_cost):
    print("Total:\t\t " + str(round(total_cost,2)))
    print("Sales tax:\t " + str(round(total_cost * .06,2)))
    print("Total after tax: " + str(round(total_cost + (total_cost * .06),2)))
    print()

def main():
    print("Sales Tax Calculator")
    print()
    choice = "y"
    while choice.lower() == "y":
        get_cost()

        # see if the user wants to continue
        choice = input("Again? (y/n): ")
        print()

    print("Thanks, bye!")        
    
if __name__ == "__main__":
        main()