运行此操作时遇到一些麻烦。基本上我只想得到一笔金额,如果它超过50免费送货,少于10美元的额外费用。我继续收到关于将浮点数转换为str implicity的错误?我认为我的输入应该被视为浮动?
#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.")
答案 0 :(得分:1)
问题在于您的打印声明:
print("Your purchase amount is " + purchaseAmount + "$ and shipping is free!")
您在没有转换的情况下连接字符串和浮点数,请尝试向变量添加str()强制转换:
即:
print("Your purchase amount is " + str(purchaseAmount) + "$ and shipping is free!")
您还可以使用格式:
print("Your purchase amount is {0}$ and shipping is free!".format(purchaseAmount))
答案 1 :(得分:0)
答案 2 :(得分:0)
只需用str(purchaseAmount)替换purchaseAmount,你就可以了。
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.
答案 3 :(得分:0)
您输入的是浮点数,因此无法与字符串连接。你必须插入浮动。
print('Your purchase amount is ${} and shipping is free!'.format(purchaseAmount))