Python Forum
Resolving unsupported operand type +=: 'int' and 'str' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Resolving unsupported operand type +=: 'int' and 'str' (/thread-21011.html)



Resolving unsupported operand type +=: 'int' and 'str' - jedmond2 - Sep-10-2019

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'



RE: Resolving unsupported operand type +=: 'int' and 'str' - ichabod801 - Sep-10-2019

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.


RE: Resolving unsupported operand type +=: 'int' and 'str' - jedmond2 - Sep-10-2019

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.


RE: Resolving unsupported operand type +=: 'int' and 'str' - ichabod801 - Sep-10-2019

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.


RE: Resolving unsupported operand type +=: 'int' and 'str' - jedmond2 - Sep-10-2019

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