0

I got a new laptop recently, and its OS is windows 11. I was testing some stuff, i wrote a simple python code, to just print "hello", and run it on the command prompt. When i run the code, a new cmd window rapidly opens and closes. After that i put a loop on the code and input prompts, and the window stayed open until the code ended. I tried running the code with errors. It runs until the error line and closes the window, not showing me any errors. I dont know why this is happening, but i think it might have something to do with windows 11, as i never had this type of problem whith windows 10.

I tried running simple python code on the cmd, and i expected it to run normally, but instead a new cmd window opens and closes, not showing me the output of the code.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Rafles
  • 1

2 Answers2

2

It is the default behavior in Windows. In some cases, it is good as many people try to run automated scripts by Python so they want it to not have any user interactions. But in your case, you can add an input() statement at the end of your code. So when the code ends you need to enter any key to pass this line and as it is the last line, it ends the program.

I suggest using a good input message so it makes sense; like: input('press any key to exit')

  • the problem is, even when the code have errors, it just closes the window without any error message – Rafles May 01 '23 at 01:09
0

As previously mentioned, you can use the input() statement at the end of your code. Alternatively, you can run your code through powershell directly with the command:
py path/yourpythonfile.py