1

I use notepad++ to write my python code, and when I put in this code for a basic calculator:

However, after the input calls are over, it auto-closes the console window, supposedly because it finishes the rest of the calculations extremely quickly, but how do I stop it from closing?`I use notepad++ to write my python code, and when I put in this code for a basic calculator:

num1 = input("Enter a number") num2 = input("Enter another number") result = float(num1) + float(num2) print(result) 

However, after the input calls are over, it auto-closes the console window, supposedly because it finishes the rest of the calculations extremely quickly, but how do I stop it from closing?

7
  • 3
    Does this answer your question? How to keep a Python script output window open? Commented Nov 28, 2020 at 10:35
  • The problem isn’t Python, it’s Notepad++. I’m extremely surprised that Notepad++ has such a glaring bug, but from a quick search this indeed seems to be the case. So the solution is: use a proper editor. Commented Nov 28, 2020 at 10:38
  • 2
    @KonradRudolph Umm.. No? If you ran this Python script standalone the exact same thing would happen - thats just how python works. Once its finished it closes the window. The has absolutely nothing to do with Notepad++, given that the program simply runs the standard python command anyway. Commented Nov 28, 2020 at 10:42
  • @dantechguy No, Python does none of these things. The behaviour is due to cmd.exe, not due to Python. If you run the Python script standalone inside a regular terminal it works. And that’s how command-line applications are supposed to be run. If you want to run an application that opens an interactive window by double-clicking, and keeps it open, don’t write a command-line application; write a GUI application. Commented Nov 28, 2020 at 10:46
  • @KonradRudolph Here's an Official Python page describing the exact situation above. I completely agree, what you've described is how command line applications should be run. But in cases like this, I feel its more helpful to answer the user's question in an accessible way, over a 100% technically correct one. Commented Nov 28, 2020 at 10:54

2 Answers 2

1

You're completely right, as soon as Python finishes it sees no need to keep the console window open, so the trick is to make it keep doing something. That way it hasn't "finished", and won't close.

If you add

input() 

to the last line of your program, Python will wait until you press Enter, therefore keeping the console window open until you're ready :)

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

2 Comments

That’s a terrible solution. The issue isn’t with Python, it’s the editor. This hack breaks the code’s correct behaviour to work around a bug in the editor.
This is completely false. Has nothing to do with Notepad++ at all. Same thing will happen if you run the python file directly. This is determined by what console terminal is used, in Windows would be cmd.exe, which will close instantly if it doesn't wait for input.
0

You can have an input function at the end of the file. Then it'll halt until the user presses enter.

num1 = input("Enter a number") num2 = input("Enter another number") result = float(num1) + float(num2) print(result) input('Press the Enter key to exit...') 

21 Comments

That’s a terrible solution. The issue isn’t with Python, it’s the editor. This hack breaks the code’s correct behaviour to work around a bug in the editor.
@KonradRudolph Why would this be a bug with Notepad++? Notepad++ simply executes the Python scripts, and if you execute the Python script the console will close immediately. Other editor may execute the script in terminal, etc., but that's just a different way of doing things, not a broken way.
@Holt Because Notepad++ purports to be an IDE with built-in support for executing command-line applications. If it fails to keep the console open after the application has terminated, that’s a bug. Of course the real issue is that cmd.exe is idiotic. But this is known, and if an editor wants to use it productively, it simply has to account for that fact.
No, it's not a problem with the editor. The editor is not a part of a running program. I've done something similar when running C++ code in Visual Studio as well.
@TedKleinBergman Just because other programs have the same issue (even extremely big ones, like VS) doesn’t make this less of a problem of the editor. The behaviour of cmd.exe is well-known. NP++ can’t just say “so what?” and ignore that. Anyway, VS at least does work around this issue (= running the application in debug mode).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.