Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can only concatenate str (not "float") to str

Tags:

python

I'm trying to make a program that compares the density of a certain mass and volume to a list of densities of compounds, and return the type of compound I am analyzing.

This is the part of the code that is returning an error:

peso = float(input("Qual o peso do plastico da sua protese?"))
volume = float(input("Qual o volume do material?"))
str(peso)
str(volume)

def resultados():
  print('O peso do plastico é de ' + peso, end="", flush=True)

resultados()
  print(' g e tem um volume de ' + volume + "dm^3")

The error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-9-d36344c01741> in <module>()
      8     print('O peso do plastico é de ' + peso, end="", flush=True)
      9 
---> 10 resultados()
     11 print(' g e tem um volume de ' + volume + "dm^3")
     12 #############

<ipython-input-9-d36344c01741> in resultados()
      6 
      7 def resultados():
----> 8     print('O peso do plastico é de ' + peso, end="", flush=True)
      9 
     10 resultados()

TypeError: can only concatenate str (not "float") to str
like image 495
J_Zoio Avatar asked Oct 13 '18 19:10

J_Zoio


People also ask

Can only concatenate str not list to STR?

The Python "TypeError: can only concatenate str (not "list") to str" occurs when we try to concatenate a string and a list. To solve the error, access the list at a specific index to concatenate two strings, or use the append() method to add an item to the list.

Can only concatenate str not float to STR in Django?

The expression is generated using the operators and the operands. There is a need to work together with operators on different types of operands. The error 'TypeError: can only concatenate str (not “float”) to str' is due to improper use of the various operands with python operators.

Can only concatenate list not float to list error?

The Python "TypeError: can only concatenate list (not "float") to list" occurs when we try to concatenate a list and a floating-point number. To solve the error, use the append() method to add an item to the list, e.g. my_list. append(3.3) .

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). Here is an example of how the error occurs.

What does TypeError can only concatenate Str to STR mean?

TypeError: can only concatenate str (not “int”) to str as the name is evident occurs when a value other than str type is tried to concatenated together. Often, beginners face this issue; however, don’t fret; it is easy to resolve.

How to solve the error when converting a float to string?

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). Here is an example of how the error occurs.

How to convert float to string in Python?

You can use str () function to help convert float to string in Python. See example below:


1 Answers

You have some options about how to go about this

Using peso = str(peso) and same for volume = str(volume)

peso = float(input("Qual o peso do plastico da sua protese?"))
volume = float(input("Qual o volume do material?"))
peso = str(peso)
volume = str(volume)

def resultados():
  print('O peso do plastico é de ' + peso, end="", flush=True)

resultados()
print(' g e tem um volume de ' + volume + "dm^3")

Or you could just convert them to str when you are performing your print this way you can preserve the values as floats if you want to do more calculations and not have to convert them back and forth over and over

peso = float(input("Qual o peso do plastico da sua protese?"))
volume = float(input("Qual o volume do material?"))

def resultados():
  print('O peso do plastico é de ' + str(peso), end="", flush=True)

resultados()
print(' g e tem um volume de ' + str(volume) + "dm^3")
like image 169
vash_the_stampede Avatar answered Sep 29 '22 23:09

vash_the_stampede