+ 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.
1 Respuesta
+ 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).