Python Forum
Resolving unsupported operand type +=: 'int' and 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resolving unsupported operand type +=: 'int' and 'str'
#1
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'
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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()
 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unsupported operand type(s) for /: 'str' and 'int' enderfran2006 1 2,597 Oct-01-2020, 09:41 AM
Last Post: buran
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,076 Oct-20-2019, 06:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020