0

I keep getting an error when I run this code that says it can only concantenate str (not int) to str. I'm extremely new to coding and well aware I've probably made a super obvious mistake but would like help pointing me in the right direction as to how I can resolve this for my little program.

T = 0
def amount(y, x = .0925):
    tax = y * x
    taxed = tax + float(y)
    taxed = round(taxed, 2)
    taxed = str(taxed)
    return taxed
user_input = ""
while user_input != "q":
    Cost = input("How Much Does It Cost? q to quit ")
    if Cost != "q":
        y = float(Cost)
        z = amount(y)
        z += T
        print("Your Total is " + z)
    if Cost == "q":
        break

Would greatly appreciate someone pointing out the (I'm sure) incredibly obvious thing I'm doing wrong here.

edit** Thank you so much! after a quick edit to my code I was able to get this to run and function the way I wanted. I knew it had to be something obvious I didn't have enough experience to see.

T = 0
def amount(y, x = .0925):
    tax = y * x
    taxed = tax + float(y)
    return round(taxed, 2)
user_input = ""
while user_input != "q":
    Cost = input("How Much Does It Cost? q to quit ")
    if Cost != "q":
        y = float(Cost)
        z = amount(y)
        T += z
        z = str(T)
        print("Your Total is " + z)
    if Cost == "q":
        break
  • Does this answer your question? [Getting a TypeError: can only concatenate str (not "int") to str](https://stackoverflow.com/questions/51252580/getting-a-typeerror-can-only-concatenate-str-not-int-to-str) – bdrx Feb 20 '20 at 22:08
  • Be sure to do give a little effort and do some basic research before posting questions on stackoverflow. See https://stackoverflow.com/help/how-to-ask. The error message should have told you what line your problem is on. You are trying to add int `T` to a str `z` on `z += t`. – bdrx Feb 20 '20 at 22:11

1 Answers1

0

Your function amount returns a string rather than a number, so you need to alter your function to e.g.

def amount(y, x = .0925):
    tax = y * x
    taxed = tax + float(y)
    return round(taxed, 2)

And then further down

   z = amount(y)
   z += T
   print("Your Total is " + z)

Otherwise you're trying to add bananas to apples (string to integers).

Jan
  • 42,290
  • 8
  • 54
  • 79