Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7393] Executing Python program of sum of 2 nos.

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 24, 2009, 11:09 PM

Post #1 of 2 (164 views)
Permalink
[issue7393] Executing Python program of sum of 2 nos.

New submission from Mayuresh <mayur_marathe [at] hotmail>:

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.

----------
components: IDLE
messages: 95711
nosy: mayur78
severity: normal
status: open
title: Executing Python program of sum of 2 nos.
type: behavior
versions: Python 3.1

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7393>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 24, 2009, 11:19 PM

Post #2 of 2 (161 views)
Permalink
[issue7393] Executing Python program of sum of 2 nos. [In reply to]

Raymond Hettinger <rhettinger [at] users> added the comment:

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)

----------
nosy: +rhettinger
resolution: -> invalid
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7393>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.