Our geometry teacher gave us an assignment asking us to create an example of when toy use geometry in real life, so I thought it would be cool to make a program that calculates how many gallons of water will be needed to fill a pool of a certain shape, and with certain dimensions.
Here is the program so far:
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.")
i keep getting this error though:
easygui.msgbox=("You need "+(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.") TypeError: cannot concatenate 'str' and 'float' objects
what do i do?
Python concatenate string and float In Python, We cannot use the + operator to concatenate one string and float type. We cannot concatenate a string with a non-string type. We will use str() to convert the float to string type and then it will be concatenated.
If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.
The Python "TypeError: can only concatenate str (not "float") to str" occurs when we try to concatenate a string and a float. To solve the error, convert the float to a string, e.g. str(my_float) to concatenate the strings or convert the str to a float, e.g. float(my_str) .
In Python, we cannot concatenate a string and an integer together. They have a different base and memory space as they are completely different data structures.
All floats or non string data types must be casted to strings before concatenation
This should work correctly: (notice the str
cast for multiplication result)
easygui.msgbox=("You need "+ str(3.14*(float(radius)**2) * float(height)) + "gallons of water to fill this pool.")
straight from the interpreter:
>>> 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.
There is one more solution, You can use string formatting (similar to c language I guess)
This way you can control the precision as well.
radius = 24 height = 15 msg = "You need %f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height)) print(msg) msg = "You need %8.2f gallons of water to fill this pool." % (3.14 * (float(radius) ** 2) * float(height)) print(msg)
without precision
You need 27129.600000 gallons of water to fill this pool.
With precision 8.2
You need 27129.60 gallons of water to fill this pool.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With