+ 2

Why conversation into float is necessary here.....

num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result)

6th Apr 2018, 6:43 AM
ALBERT SAURAV
ALBERT SAURAV - avatar
2 ответов
+ 12
input returns a string type. If you just simply add them with a + they will get added string-wise -- will be concatenated. If you want a numerical addition, you have to convert to a numerical type - either float or integer.
6th Apr 2018, 6:50 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
You are already converting the input upon the user entering it. Just remove the str() conversion for the result variable and it should work fine... result = num1 + num2 Explanation: You want to add the two floats together. Since they are already floats, you don't need any further conversion. You are converting the floats back to strings, nullifying the float conversion you performed on the inputs.
6th Apr 2018, 6:59 AM
synorax
synorax - avatar