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


Messages In This Thread
Resolving unsupported operand type +=: 'int' and 'str' - by jedmond2 - Sep-10-2019, 02:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unsupported operand type(s) for /: 'str' and 'int' enderfran2006 1 2,664 Oct-01-2020, 09:41 AM
Last Post: buran
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,198 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