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
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.
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.
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) .
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.
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.
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.
You can use str () function to help convert float to string in Python. See example below:
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")
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