2013-06-05 10 views
20

Il nostro insegnante di geometria ci ha dato un incarico chiedendoci di creare un esempio di quando il giocattolo usa la geometria nella vita reale, quindi ho pensato che sarebbe stato bello creare un programma che calcoli quanti galloni d'acqua saranno necessari per riempire una piscina di una certa forma e con determinate dimensioni.Non è possibile concatenare oggetti 'str' e 'float'?

Ecco il programma finora:

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.") 

io continuo a ricevere questo errore se: easygui.msgbox = ("devi" + (3.14 * (float (raggio) ** 2) * galleggiante (altezza)) + "litri d'acqua per riempire questa piscina.") TypeError: impossibile concatenare gli oggetti 'str' e 'float'

cosa devo fare?

risposta

31

Tutti galleggianti o tipi non di dati stringa deve essere fusa in stringhe prima di concatenazione

Questo dovrebbe funzionare correttamente: (notare il cast str per risultato la moltiplicazione)

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

direttamente dal interprete:

>>> 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. 
Problemi correlati