-1

I click enter to enter input data and it closes. I am using windows ten, new computer, not glitchy. code is

import os

printput = input("Do: ")
if printput == "send message":
  myinput = input("s:")
  print (myinput)
  pass
elif printput == "clear term":
  os.system("clear")
  pass

what I do, "Do: send message, Enter, s:random stuff, Enter, #window closes#"

J Bergh
  • 11

1 Answers1

0

I don't know how you are running your file, but probably at the end of the execution the terminal is closing automatically, to avoid that you can open the windows terminal, navigate to where your file is stored using cd for example cd Desktop and then start your file using the command python filename.py that way the terminal won't close even after your file finishes executing. A better solution that will help you in developing what seems to be a shell that executes your commands is using a while True: at the beginning. This way no matter how you execute your file, terminal won't close. and your script will keep running your commands until you close it, And your code will become like this :

import os

while True:
    printput = input("Do: ")
    if printput == "send message":
        myinput = input("s:")
        print (myinput)
        pass
    elif printput == "clear term":
        os.system("cls")
        pass

this way your code will keep running forever and asking you for commands and executing them, and i changed os.system("clear") because it doesn't work, to os.system("cls") the command that clears the terminal.

another thing you can do to avoid terminal from closing at the end of execution is to make it wait for a keypress by adding at the end of your code

input("Press Enter to continue...")
Elyes Lounissi
  • 405
  • 3
  • 12