classification
Title: Executing Python program of sum of 2 nos.
Type: behavior Stage:
Components: IDLE Versions: Python 3.1
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mayur78, rhettinger
Priority: normal Keywords:

Created on 2009-11-25 07:09 by mayur78, last changed 2009-11-25 07:19 by rhettinger. This issue is now closed.

Messages (2)
msg95711 - (view) Author: Mayuresh (mayur78) Date: 2009-11-25 07:09
I am getting error of sum of two nos. the output should give addition 
of two numbers,but instead it is displaying 2 nos. side-by-side.I have 
pasted the program and output for the same.

print "Please give a number: "
a = input()
print "And another: "
b = input()
print "The sum of these numbers is: "
print a + b

output should be a=21,b=21, a+b=42
whereas it is showing 2121.
msg95712 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-25 07:19
In Python 3.1, the input() function always returns a string.  In your
case, the string is "21".  Adding two strings together gives you a
longer string:  "12" + "34" --> "1234".

To get your program to do what you want, convert the string to a number
using int():

a = int(input('give a number: '))
b = int(input('and another: '))
print('the sum is', a + b)
History
Date User Action Args
2009-11-25 07:19:22rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg95712

resolution: not a bug
2009-11-25 07:09:53mayur78create