3 回答

TA貢獻1829條經驗 獲得超9個贊
在連接之前,必須將所有浮點數或非字符串數據類型強制轉換為字符串
這應該可以正常工作:(請注意str強制轉換為乘法結果)
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
直接來自口譯員:
>>> radius = 10
>>> height = 10
>>> msg = ("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
>>> print msg
You need 3140.0gallons of water to fill this pool.

TA貢獻1848條經驗 獲得超2個贊
使用Python3.6 +,您可以使用f字符串格式化打印語句。
radius=24.0
height=15.0
print(f"You need {3.14*height*radius**2:8.2f} gallons of water to fill this pool.")

TA貢獻1815條經驗 獲得超6個贊
還有另一種解決方案,您可以使用字符串格式設置(類似于我猜的C語言)
這樣,您也可以控制精度。
radius = 24
height = 15
msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height))
print(msg)
沒有精度
您需要27129.600000加侖水來填充該池。
精度8.2
您需要27129.60加侖的水來填充該池。
添加回答
舉報