+ 1

If I can add an integer and a float, why do we have to convert the float to avoid an error?

If I can say "print(1 + 2.0)" and get 3.0, then why do I get an error in the following code if I remove the second "float" conversion? print(float(input("Enter a number: ")) + (float(input("Enter another number: "))

24th Feb 2018, 6:17 AM
Austin Hofmann
Austin Hofmann - avatar
4 Answers
+ 9
Because input() returns a string, if you don't cast the string to a float type you will be adding a float value with a string. Hth, cmiiw
24th Feb 2018, 6:23 AM
Ipang
+ 5
You're welcome mate : )
24th Feb 2018, 6:26 AM
Ipang
+ 2
Bingo! That answers my question perfectly. Thanks!
24th Feb 2018, 6:25 AM
Austin Hofmann
Austin Hofmann - avatar
+ 1
That's my wife. Unfortunately she isn't a programmer and couldn't help with my question. Bummer!
24th Feb 2018, 6:21 AM
Austin Hofmann
Austin Hofmann - avatar