0

Using different consoles shows different results ..

Hello, I'm trying with the following code: elif user_input == "add" num1 = float(input ("Enter the first value: ")) num2 = float(input ("Enter the second value: ")) result = str(num1 + num2) print("The result is: " + result) If I run on Spyder Phyton 3.6 on my laptop I get concatenated numbers. With inputs 1 and 2, I got 12. If I run on online console or in this app I got 3. This is frustrating.. What is the reason for that and what is the general rule to follow? Thanks.

4th Sep 2018, 5:16 AM
Ivan Georgiev
Ivan Georgiev - avatar
3 ответов
+ 3
Calculate result first, use str() inside print() as follows: result = num1 + num2 print("The result is: " + str(result)) Or, use format() as follows: result = num1 + num2 print("The result is: {}".format(result)) This also will work: print("The result is: {}".format(num1 + num2)) Hth, cmiiw
4th Sep 2018, 6:11 AM
Ipang
+ 1
Thanks! Both ways work!
4th Sep 2018, 5:08 PM
Ivan Georgiev
Ivan Georgiev - avatar
+ 1
You're welcome, glad if it helps : )
4th Sep 2018, 5:23 PM
Ipang