why when i enter that code get '102' and not 12.0 the code is: input("enter a number: ") + input ("enter another number: ") enter a number: 10 enter another number: 2 why i get '102'. and not 12 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why when i enter that code get '102' and not 12.0 the code is: input("enter a number: ") + input ("enter another number: ") enter a number: 10 enter another number: 2 why i get '102'. and not 12

i know that i can enter int(function) or float but i wanna to understand why the output is 102

6th Jul 2016, 9:36 PM
Ahmed Kamal
Ahmed Kamal - avatar
4 Answers
+ 3
You concatenated the two strings. Don't forget that input () on its own returns a string value. So for the first input, you entered 10. The input function returns it as a string. The second when you entered 2 is returned as a string too. So when you use the + operator on two strings it concatenates them. "10"+"2" becomes "102". If you want the values to be treated as numbers, you must convert the result from the input to an integer using int () or float () if you want the value to be a decimal number.
6th Jul 2016, 10:32 PM
Gershon Fosu
Gershon Fosu - avatar
+ 1
because the program uses 10 and 2 as strings, not as ints. It is simply putting "10" and "2" together to form "102" instead of doing 10+2=12
6th Jul 2016, 10:25 PM
Ethan Graber
Ethan Graber - avatar
+ 1
Input is interpreted like a str string, the + command join the two str, no the logical operation of addiction 1+1
6th Jul 2016, 10:27 PM
Carlos Valle Seldas
Carlos Valle Seldas - avatar
0
thanks alot guys :)
6th Jul 2016, 10:46 PM
Ahmed Kamal
Ahmed Kamal - avatar