I`m stuck in this code. Always getting an error when I`m trying to execute this code. (I`m beginner in python)

Question:

Current Code:

weight_lbs=float(input("How much do you weigh(lbs)? : " ))
weight_kg=float(input("My weight is " + weight_lbs/2.205 + "kg"))
print(weight_kg)

Error Message:

Traceback (most recent call last):
File "<string>", line 9, in <module>
TypeError: can only concatenate str (not "float") to str
Asked By: Shahi

||

Answers:

You cannot add string with a floating value

So, solution is to convert the floating value to string and then concatenate

weight_lbs=float(input("How much do you weigh(lbs)? : " ))
weight_kg=float(input("My weight is " + str(weight_lbs/2.205) + "kg"))
print(weight_kg)
Answered By: sourin
weight_lbs = float(input("How much do you weigh(lbs)? : " ))

kg = str(weight_lbs/2.205)

print("My weight is " + kg + "kg")
Answered By: Elusive_DODO

Trying to run your code, the following error appears:

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

this is because, you are trying to concatenate a string with a number of type float precisely. Concatenation can only occur between strings, so you need to convert this data type and in python you simply do this with str() as follows:

weight_lbs=float(input("How much do you weigh(lbs)? : " ))
weight_kg=float(input("My weight is " + str(weight_lbs/2.205) + "kg"))
print(weight_kg)
Answered By: Giuseppe La Gualano
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.