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

2
  • 1
    try print instead of Print. Commented Jun 2, 2013 at 10:36
  • 1
    @segfolt thats a typo while writing the question, sorry Commented Jun 2, 2013 at 10:37

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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


2 Comments

+1 for bringing out an image ;)
I do what I can. haha
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

4 Comments

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 "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
The OP is using 2.7.3.
@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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.