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
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)
weight_lbs = float(input("How much do you weigh(lbs)? : " ))
kg = str(weight_lbs/2.205)
print("My weight is " + kg + "kg")
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)
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
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)
weight_lbs = float(input("How much do you weigh(lbs)? : " ))
kg = str(weight_lbs/2.205)
print("My weight is " + kg + "kg")
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)