2015-06-09 128 views
0

在運行時遇到了一些麻煩。基本上我只想得到一個金額,如果它超過50免費送貨,少於收取額外10美元。我繼續得到關於將float轉換爲str隱含的錯誤?我認爲我的意見應該被認爲是一個浮動?爲什麼我無法隱式地將float轉換爲str?

#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

你可以通過谷歌搜索你的問題的標題得到答案。 – TigerhawkT3

回答

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)) 
+0

真棒謝謝你的幫助! – Aaron

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. 
0

您輸入的是一個浮點數,因此它不能與字符串連接。你必須插入浮點數。

print('Your purchase amount is ${} and shipping is free!'.format(purchaseAmount)) 
相關問題