Type conversion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Type conversion

Another example of type conversion is turning user input (which is a string) to numbers (integers or floats), to allow for the performance of calculations. float(input("Enter a number: ")) + float(input("Enter another number: ")) Enter a number: 40 Enter another number: 2 42.0 I did n't understand how this works and i have also no clarity about how "input" works. when the above statement entered in code playground it is displaying error where as the example says you get 42.0 with 40 & 2 by using the above combination statement of print, float & input.

20th May 2020, 3:11 PM
Somaraju Rajasekhar
Somaraju Rajasekhar - avatar
1 Answer
+ 6
You forgot the print statement to display the result. Try this: a = float(input('Enter a number: ')) b = float(input('Enter another number: ')) print(a+b) If you don't convert, both variables would remain of string type and printing a+b would result in: 402 (assuming you entered 40 and 2, respectively).
20th May 2020, 3:15 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar