r/pythonhelp • • Apr 28 '22

HOMEWORK i keep getting this message saying that i cant multiply a sequence with a non-int of type

code:

burger = int(input("Enter the number of burger:"))

fries = int(input("Enter the number of fries:"))

drink = int(input("Enter the number of drink:"))

total = str((burger * 1.49) + (fries * 0.89) + (drink * 0.99))

tax = int(float(total)) * (int(0.07))

Gtotal = str(total) + str(0.07)

print(("The total before tax is : $") + str(total))

print(("the tax is : $") + (str(total)) * (str(0.07)))

print("The grand total is : $") + str(Gtotal)

tendered = int(input("Enter amount tendered: "))

change = str(Gtotal) - str(tendered)

print("The change is: $") + float(str(change))

error message:

Traceback (most recent call last):

File "main.py", line 18, in <module>

print(("the tax is : $") + (str(total)) * (str(0.07)))

TypeError: can't multiply sequence by non-int of type 'str'

if anyone can find the solution to the error please reply. thank you for the help

:)

3 Upvotes

5 comments sorted by

2

u/MT1961 Apr 28 '22

Uh, what are you trying to do here? If you want to figure out the tax, why did you convert total to a string in the first place?

consider:
total = (burger * 1.49) + (fries * 0.89) + (drink * 0.99)

tax = total * 0.07

print("Tax = ${}".format(tax))

1

u/Smoll_boi69 Apr 29 '22

Sorry if it’s complicated it what my teacher wants

2

u/skellious Apr 29 '22
print(("the tax is : $") + (str(total)) * (str(0.07)))

this will never work becuase you cannot multiply two strings together. for example, what would be the answer to:

"hello" * "goodbye"

it doesn't make sense, does it?

you can do this:

print("the tax is: $" + str(total*0.07))

or even better, follow modern convention and use f-strings:

print(f"the tax is: ${total*0.07}")

2

u/throwaway8u3sH0 Apr 29 '22 edited Apr 30 '22

I bet the teacher wants him to explicitly cast everything in order to learn about the differences in types. So like str(int(5)+float(5.75)). Mixing those up would cause various failures.

3

u/CraigAT Apr 28 '22

The error message is because on the line suggested you are attempting to multiply two strings.

As the other comment suggests, keep everything in number (int or float) until you need to display it.