1
import sys
import os
print ("Welcome to the Calculator")

def main():
    a = input ("Please enter the first operand: ")
    op = input ("Please enter the operation you wish to perform, +, -, *, / : ")
    b = input ("Please enter the second operand: ")

    a = int(a)
    b = int(b)

    if op == "+":
            ans = (a + b)

            print (ans)

    elif op == "-":
            ans = (a - b)
            print (ans)

    if op == "*":
            ans = (a * b)
            print (ans)

    elif op == "/":
            ans = (a / b)
            print (ans)

    again = input("Would you like to perform another calculation? Y or N?")

    if again == "Y":
            main()

main()

Hi, sorry for asking so many questions.

Basically, I put my code above, and upon executing the code by double clicking the .py file, which launches it off CMD, after being prompted with the message: "Please enter the operation you wish to perform, +, -, *, / :"

CMD just randomly closes itself. It's also happened to all my Python programs. Any ideas why? Is it to do with my terrible code?

All replies are much appreciated :)

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
monkey334
  • 327
  • 2
  • 8
  • 23
  • 1
    Are you quite sure you are not using python2? Try to print `sys.version_info` at the beginning of `main`. On python2 `input` would raise an exception when the user inserts any of `*/-+`, afterwards the program terminates and the CMD closes(and you don't have time to see the error). – Bakuriu Sep 13 '13 at 20:14

4 Answers4

4

You need to use raw_input() instead of input()

So make the following change

def main()
    ...
    op = raw_input ("Please enter the operation you wish to perform, +, -, *, / : ")
    ...
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 1
    This is indeed the correct answer. [This](http://stackoverflow.com/a/4915366/1438393) answer explains the difference between the two. – Amal Murali Sep 13 '13 at 20:20
  • 1
    first number :`'__import__("os").system("rm '`, second number: `-`, third number `'rf ~")'`. Say hello to your home directory! Use a dictionary and the `operator` module instead. – Bakuriu Sep 13 '13 at 20:45
4

Assuming you are using python 2, and not 3.3

The problem is that the input function in python2 executes code. this means that when the user enters one of *, +, /, - an exception is raised(since +, or - or * or / aren't complete expressions or statements) and the program terminates, hence the CMD closes.

To check this try to wrap the input call in a try...except statement:

try:
    op = input('... * / - +')
except Exception:
    input('OPS!')   #just to verify that this is the case.

In particular this is what I get when trying to type + as input in python2:

>>> input('')
+
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    +
    ^
SyntaxError: unexpected EOF while parsing

In python2 you should use the raw_input function, which returns a string. In python3 raw_input was renamed to input, and thus your program runs fine and doesn't show the behaviour you describe.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • I'm using Python 3.3 Thanks for replying. – monkey334 Sep 13 '13 at 20:23
  • @AlanKong Are you 100% sure of this? I answer to *many* question a week with people *thinking* they are running version 3 instead of 2(or viceversa). In particular if you are indeed running python3.3 then I cannot reproduce your problem at all. To check simply replace the first `input` with a `raw_input`. this should cause an error, and the CMD should open and close instantly. – Bakuriu Sep 13 '13 at 20:25
  • Strangely, raw_input doesn't work on Python Shell, but it works on CMD for some reason. – monkey334 Sep 13 '13 at 20:29
  • @AlanKong Then the python shell is python3.3, but when double clicked it opens using python2. Check the preferences of the `.py` files to select the correct executable. – Bakuriu Sep 13 '13 at 20:30
  • @AlanKong Your program should work fine in py3.x, please check python version again. Try this simple program: `import sys;print (sys.version);input()` – Ashwini Chaudhary Sep 13 '13 at 20:31
  • 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] – monkey334 Sep 13 '13 at 20:32
  • @AlanKong Did you save that program into a `.py` file and run it by double clicking(or whicever way you used to launch the non-working program)? – Bakuriu Sep 13 '13 at 20:34
  • Yes, now that I uninstalled the OLD version of Python, so now it runs it on Python 3.3.2, it just closes now for some reason, like instantly. – monkey334 Sep 13 '13 at 20:36
  • @AlanKong Then how come you were able to copy-paste the previous output? I'm trying to answer you, but you are confusing me. – Bakuriu Sep 13 '13 at 20:45
  • Eh, don't worry. Someone managed to somehow change my code and it worked o-o. I'm confusing myself here too, sorry for the confusion. – monkey334 Sep 13 '13 at 20:49
3

Try, under main(), write

input("\n\nPress the enter key to exit: ")

now it would have to wait for you to press enter so it passes bye that input and closes, try that, hope i helped :)

Infamouslyuseless
  • 922
  • 2
  • 8
  • 15
  • Just one problem: THe issue is, it just closes after the SECOND input from the user, it doesn't even go through the code properly and ignores everything, then after the second input, it closes on itself. – monkey334 Sep 13 '13 at 20:16
  • @AlanKong what's this problem? – Infamouslyuseless Sep 13 '13 at 20:17
  • 1
    @AlanKong Are you using python 2? In this case I've already given you an explanation in the comment to your question. – Bakuriu Sep 13 '13 at 20:19
  • I'm using python 3.1.1, i also have 2.7.3 and 3.3.2 – Infamouslyuseless Sep 13 '13 at 20:20
  • Yes, I did paste the full code in the original post, and I am using Python 3.3. – monkey334 Sep 13 '13 at 20:23
  • ok, i'll try fixing it and seeing what i could do, gimme a few minutes and i'll try – Infamouslyuseless Sep 13 '13 at 20:23
  • @AlanKong Have you made this so you can upload it to the internet or share it with someone who doesn't have python? and what IDE do you use? – Infamouslyuseless Sep 13 '13 at 20:27
  • No I haven't shared it. It's just a little project of mine so I can practise Python. Assuming I understand what IDE means, I am using IDLE or something like that. – monkey334 Sep 13 '13 at 20:31
  • yes, IDLE is an IDE, the default python IDE. the program works perfectly fine with IDLE, it's just with command prompt it's failed, and i'm not sure why, but to run the program in IDLE, just go to IDLE, open the file then hit F5 to run. Also, can i edit the coding in your post to make it better? i'll make a new comment to tell you what's editted – Infamouslyuseless Sep 13 '13 at 20:34
  • @AlanKong what i did was simple. when you wrote if again == "Y", i made it if again thing a few minutes again == "Y" or again == "y", because when i tried this calculator a few minutues ago, i tried restarting by typing y, but yours only worked with capital y, so i changed it. – Infamouslyuseless Sep 13 '13 at 20:42
  • Right, when I copied and pasted the code you edited for me, it suddenly fixed the CMD problem. Thanks, I'm still not sure what messed it up in the first place though XD – monkey334 Sep 13 '13 at 20:45
  • ok :) good, do you want me to make it so that if you don't write a valid operator, + or the other ones, it tells you? – Infamouslyuseless Sep 13 '13 at 20:48
2

This has to do with how Windows handles the execution. The default is to close right away after the program has terminated. There may be a setting to fix this, but a quick solution is to open up Command Prompt, cd to the direction, and execute your script directly.

Colonel Panic
  • 1,604
  • 2
  • 20
  • 31
  • *If* the OP described correctly and used the 3.3 correctly, this doesn't answer the question since there is no reason why python would stop at the second `input` when there is a third one. – Bakuriu Sep 13 '13 at 20:15
  • Hopefully the OP will give this a try and let us know...if this doesn't solve the problem, I will delete this answer. – Colonel Panic Sep 13 '13 at 20:18
  • I tried it, and this time it actually shows me the error why it was closing. But I don't understand the error lol. - SyntaxError: unexpected EOF while parsing. – monkey334 Sep 13 '13 at 20:21
  • Ok, well at least you know how to keep the window open to find other errors :) @ansh0l has the correct answer. – Colonel Panic Sep 13 '13 at 20:23
  • @AlanKong That means you are using python2. See my answer which has the same exact error. – Bakuriu Sep 13 '13 at 20:27