When I open the Python program directly on CMD, it simply closes

Asked

Viewed 116 times

-1

When I try to open this program directly on CMD, without opening in CD path -> python file.py, it arrives to open, but when giving enter, it simply closes... I’ve generated an executable too, through cxFreeze, but when opening and clicking enter, it closes... Can anyone help me?

import random
import numpy


nome = input("Insira seu nome: ")
nome_do_jogo = input("Digite o nome do jogo: ")
prim_num = input("Digite o primeiro numero que pode ser jogado: ")
ult_num = input("Digite o ultimo numero que pode ser jogado: ")
qtd_numeros = input("Digite a quantidade de numeros a serem gerados: ")
mega_sena = numpy.random.randint(int(prim_num), int(ult_num), (1, int(qtd_numeros)))
mega_sena.sort()


print(nome + ", na " + nome_do_jogo + ", você pode jogar os números: " + str(mega_sena))
  • has to put a print of the command in cmd. Tested by placing python.py file and funfou

  • In CMD it goes normal, but when Gero the executable, in the first enter it closes...

1 answer

0


the point is that the program opens the cmd along with it. When it closes, the program ends, and the terminal where it was running ends together.

The way to solve this is to include a input() blank at the end of the program. Or it can be input("Pressione <enter> para encerrar!") - the input suspends the program until the user presses enter, even if it doesn’t type anything else, and this prevents the terminal from closing.

If your program gives an error before reaching this final input, it might close "instantly" yet - then the best thing to do is to open a cmd, switch to the program folder with the "cd" command and run the program with "python . py" - there, when error occurs, you will have the complete message, which points to the exact line where the error occurred.

You will find other answers, possibly in that same question, suggesting the use of os.system('pause') - This works, but personally I find it rather inelegant: the call os.system executes another program other than yours, just to wait for a key to be pressed. Simply call input perfectly solves the problem.

  • Thank you! I will do a test with input()!! Opening as "python.py file", it goes all the way, but if I open the file directly, which opens the python cmd, it instantly closes when pressing enter in the last input :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.