2013-06-05 61 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. 
相關問題