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加仑的水来填充该池。
添加回答
举报