无法连接“str”和“float”对象?

我们的几何老师给了我们一个任务,要求我们创造一个玩具在现实生活中使用几何的例子,所以我认为做一个程序来计算需要多less加仑的水才能填满一定数量的池形状和一定的尺寸。

这是迄今为止的计划:

import easygui easygui.msgbox("This program will help determine how many gallons will be needed to fill up a pool based off of the dimensions given.") pool=easygui.buttonbox("What is the shape of the pool?", choices=['square/rectangle','circle']) if pool=='circle': height=easygui.enterbox("How deep is the pool?") radius=easygui.enterbox("What is the distance between the edge of the pool and the center of the pool (radius)?") easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 

我不断收到这个错误:easygui.msgbox =(“你需要”+(3.14 *(浮动(半径)** 2)*浮动(高度))+“加仑的水来填补这个池”)TypeError:can not连接“str”和“float”对象

我该怎么办?

所有浮点数据或非string数据types必须在串联之前转换为string

这应该正常工作:(注意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.