2013-06-05 133 views
20

我们的几何老师给了我们一个任务,要求我们创建一个玩具在现实生活中使用几何的例子,所以我认为制作一个程序来计算需要多少加仑的水来填充一个池一定的形状和一定的尺寸。无法连接'str'和'float'对象?

这是迄今为止该程序:

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:无法连接'str'和'float'对象

我该怎么办?

回答

31

所有彩车或非字符串数据类型必须被强制转换为字符串连接前

这应该正常工作:直接从解释(注意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. 
相关问题