TypeError: can only concatenate str (not float) to str
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")
Error: TypeError: can only concatenate str (not float) to str
You can not append a float
to a string in Python using +
.
Only another string. That is what the error is telling you.
If you are using Python 3.6 or higher, you should use "f-strings" to show pretty output with variables, like this:
print(f'{numero} + {numero2} = {numUtilisateur}')
If you are using an older version of Python, you can format strings like this:
print('{} + {} = {}'.format(numero, numero2, numUtilisateur))
TypeError: can only concatenate str (not float)
math.sqrt
returns a float, which you are trying to append to the string "The length of diagonal:"
. Here a couple of options
Use args of print()
print("The length of the diagonal:", math.sqrt(number1**2 + number2**2))
Format the result of math.sqrt
print("The length of the diagonal: {}".format(math.sqrt(number1**2 + number2**2))
Whas might be causing 'TypeError: can only concatenate str (not int) to str' when calculating correlation?
With a little help I was able to solve this, so I am posting an answer. Maybe someone will find it useful.
The problem was indeed related to datatypes. It was caused by non-numeric values in columns SMA14 and SMA50 (those were like 'nan5', 'nan6' etc, not just 'nan'). When checking data.info(), it stated that dtype for mentioned columns are 'Objects'. And we needed them to be float64 instead.
So here what needed to be done.
1). Fix the amounts ('nan5', 'nan6' etc)
to_fix = ('SMA14', 'SMA50')
for col in to_fix:
for el in data[col]:
if (type(el) != int) and (type(el) != float) and (el[0:3] == 'nan'):
index = data[data[col] == el].index.item()
data.at[index, col] = 0
I know there are other ways of doing it, but I've ran into troubles and ultimately what worked for me, was the above. Of course, it might be that I was doing something wrong.
2). Change the datatypes - and this is what I was missing when I asked the question.
data['SMA14'] = data['SMA14'].astype(float)
data['SMA50'] = data['SMA50'].astype(float)
Related Topics
What Determines Which Strings Are Interned and When
How to Evaluate the Sum of Values Within Array Blocks
What Is the Fastest Way to Open Urls in New Tabs via Selenium - Python
When Are Objects Garbage Collected in Python
How to Import Members of All Modules Within a Package
How to Close a Thread When Multithreading
Python - Pygame Error When Executing Exe File
Tkinter Vanishing Photoimage Issue
Why Does This Not Work as an Array Membership Test
Differencebetween .Quit and .Quit in Pygame
How to Get the Row Count of a Pandas Dataframe