0

Having some trouble running this. Basically I just want to get an amount and if it's more than 50 free shipping and less than charge 10 extra dollars. I keep on getting an error about converting a float to a str implicity? I thought my input should be considered a float?

#declare flags
shippingCharge = 10
freeShipping = False
#Get number and convert to float?
purchaseAmount = float(input("\nHow much is the purchase amount? "))

if (purchaseAmount) >= 50 : 
    freeShipping = True
    print("Your purchase amount is " + purchaseAmount + "$ and shipping is free!")
else : 
    print("Your purchase amount is " + purchaseAmount + "$ and shipping is " + shippingCharge + "$.")
    purchaseAmount = shippingCharge + purchaseAmount
    print("Your new total is " + purchaseAmount)
print ("Have a nice day and thank you for shopping with us.")
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Aaron
  • 43
  • 1
  • 1
  • 9

4 Answers4

1

The problem is in your print statements:

print("Your purchase amount is " + purchaseAmount + "$ and shipping is free!")

You are concatenating string and floats without conversion, try adding a str() cast to the variable:

i.e:

print("Your purchase amount is " + str(purchaseAmount) + "$ and shipping is free!")

You can also use format:

print("Your purchase amount is {0}$ and shipping is free!".format(purchaseAmount))
dfranca
  • 5,156
  • 2
  • 32
  • 60
0

Because Python is a strongly typed language:

https://wiki.python.org/moin/Why%20is%20Python%20a%20dynamic%20language%20and%20also%20a%20strongly%20typed%20language

You need to convert the type to string explicitly. It will not do it automatically for you intentionally.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
0

Just replace purchaseAmount with str(purchaseAmount) and you will be fine.

How much is the purchase amount? 50
Your purchase amount is 50.0$ and shipping is free!
Have a nice day and thank you for shopping with us.
Alex Ivanov
  • 695
  • 4
  • 6
0

You input is a float and thus it can not be concatenated with strings. You must interpolate the float.

 print('Your purchase amount is ${} and shipping is free!'.format(purchaseAmount))
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70