+ 1
What's the reason for the totally inequal result of print(x+y) and print(float(x)+float(y))?
in python I type in the front: x=input("Enter a number.") print (x) y=input("Enter another number.") print(y) Then when I type "print(x+y)" and input "40 enter 2" it gives out the output "402", but when I type"print(float(x)+float(y))" it gives "42.0" What 's the differece except the "float" that causes the inequal result?
3 Antworten
- 1
input is a string
when adding strings you don't add the numbers but place one string after the other:
"40"+"2"="402"
while
40 + 2 = 42 (integers) or 42.0 (floats)
when you try to devide both inputs it will give an error. convert them to integer or float and you will get 20.0.
Always convert input of integers.
x = int(input())
+ 17
Default data type of input() function is string so it takes input parameters as string...
unless u convert the data type it will behave as a string...
0
402 is a concatenated string