Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot concatenate 'str' and 'float' objects?

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?

like image 711
user2443381 Avatar asked Jun 05 '13 19:06

user2443381


People also ask

Can you concatenate a string and a float?

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.

How do you concatenate str and float objects?

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.

Can only concatenate str not float to STR?

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

Why can't Python concatenate str and int objects?

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.


2 Answers

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. 
like image 171
Kalyan02 Avatar answered Sep 23 '22 03:09

Kalyan02


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.

like image 30
Gaurang Shah Avatar answered Sep 23 '22 03:09

Gaurang Shah