Simple addition program in python

34,532

Solution 1

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.

Solution 2

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).


Solution 3

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
Share:
34,532
Eka
Author by

Eka

Blah Blah Blah

Updated on July 15, 2022

Comments

  • Eka
    Eka almost 2 years

    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

  • Mr_Spock
    Mr_Spock almost 11 years
    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.
  • jamylak
    jamylak almost 11 years
    +1 for bringing out an image ;)
  • Mr_Spock
    Mr_Spock almost 11 years
    I do what I can. haha
  • lqc
    lqc almost 11 years
    @Mr_Spock "running just "myfile.py" isn't native to Windows installs." - it is since Python 3.3 which contains the launcher script: python.org/dev/peps/pep-0397
  • Mr_Spock
    Mr_Spock almost 11 years
    The OP is using 2.7.3.
  • Mike Müller
    Mike Müller almost 11 years
    @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: docs.python.org/2/using/windows.html