Using if/elif/else statements

Hi, this is likely a very basic answer that I’m some how missing. I’m trying to play around and add a little extra, but I really want to make sure I’m understanding why I’m getting this syntax error.

Here is my code:

weight = 8.4

#ground shipping
if weight <= 2:
  cost_ground = (weight * 1.50) + 20
elif weight <= 6:
  cost_ground = (weight * 3.00) + 20
elif weight <= 10:
  cost_ground = (weight * 4.00) + 20
elif weight > 10:
  cost_ground = (weight * 4.75) + 20

print("The cost to ship your package is " + cost_ground)

Output:

Traceback (most recent call last): File “shipping.py”, line 13, in print("The cost to ship your package is " + cost_ground) TypeError: must be str, not float

I appreciate any help!

your print statement is trying to add a float to a string. That’s why your getting a TypeError, “Must be str, not float”. When you concatenate both operands must be of string. To fix this
print("The cost to ship your package is " + str(cost_ground))

1 Like