5

I am trying to learn python and for that purpose i made a simple addition program using python 2.7.3

print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)

i saved the file as add.py and when i double click and run it;the program run and exits instantenously without showing answer.

Then i tried code of this question Simple addition calculator in python it accepts user inputs but after entering both numbers the python exits with out showing answer.

Any suggestions for the above code. Advance thanks for the help

Community
  • 1
  • 1
Eka
  • 14,170
  • 38
  • 128
  • 212

3 Answers3

6

add an empty raw_input() at the end to pause until you press Enter

print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)
raw_input() # waits for you to press enter 

Alternatively run it from IDLE, command line, or whichever editor you use.

jamylak
  • 128,818
  • 30
  • 231
  • 230
6

It's exiting because you're not telling the interpreter to pause at any moment after printing the results. The program itself works. I recommend running it directly in the terminal/command line window like so:

screenshot of it working

Alternatively, you could write:

import time

print("Enter two Numbers\n")
a = int(raw_input('A='))
b = int(raw_input('B='))
c=a+b
print ('C= %s' %c)
time.sleep(3.0) #pause for 3 seconds

Or you can just add another raw_input() at the end of your code so that it waits for input (at which point the user will type something and nothing will happen to their input data).


Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
2

Run your file from the command line. This way you can see exceptions.

Execute cmd than in the "dos box" type:

python myfile.py

Or on Windows likley just:

myfile.py
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • There are no exceptions here. Also, running just "myfile.py" isn't native to Windows installs. You have to set up your environment variables for running Python scripts this way. By default, you still need to prefix each file with "python" to run them. – Mr_Spock Jun 02 '13 at 11:19
  • @Mr_Spock "running just "myfile.py" isn't native to Windows installs." - it is since Python 3.3 which contains the launcher script: http://www.python.org/dev/peps/pep-0397/ – lqc Jun 02 '13 at 11:22
  • The OP is using 2.7.3. – Mr_Spock Jun 02 '13 at 11:23
  • @Mr_Spock Using `Print` instead of `print`, as in the original version, will cause an exception. To my experience it is just the other around on windows. The extension `.py` is associated with the Python executable but typing `python` does not work for Python 2.7 out of the box: http://docs.python.org/2/using/windows.html – Mike Müller Jun 02 '13 at 11:26