2013-06-05 5 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'オブジェクトを連結できません

私は何をしますか?ストレートインタプリタから

easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") 

(乗算結果のstrキャストに注意してください):

答えて

31

は、すべての山車や非文字列データ型は、これが正常に動作するはず

連結前に文字列にキャストする必要があります

>>> 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. 
関連する問題