2

I would like to have an input loop in python 3 where the information which gets typed in gets deleted from terminal automatically (f.eks. after 3 seconds) I know the function with \r to go back in line, but struggle with the automatic new line after input.

while True:
    inputStr = (input("Add the hidden word: ")).lower()
    processingTheInput(inputStr) #some sort of function using the input word
  • take a look at the `curses` module – AntiMatterDynamite Jan 14 '20 at 15:02
  • what output are you expecting – Ironkey Jan 14 '20 at 15:13
  • 1
    @ironkey We use this program for gaming evenings, where every player can put words into an array without others knowing the word, so we give the program around. Afterwards somebody „draws“ a random element from the array. Therefore I want the input to disappear after somebody typed it in –  Jan 14 '20 at 15:34

2 Answers2

2

Ansi escape codes will not work the same on all terminals but this might suit your needs. The ‘\033’ is the escape character. The ‘[1A’ says go up one line and the ‘[K’ says erase to the end of this line.

prompt = 'Add the hidden word: '
inputStr = input(prompt).lower()
print ('\033[1A' + prompt + '\033[K')
bashBedlam
  • 1,402
  • 1
  • 7
  • 11
0

You want to clear the terminal with a function

# import only system from os 
from os import system, name 

# import sleep to show output for some time period 
from time import sleep 

# define our clear function 
def clear(): 

    # for windows 
    if name == 'nt': 
        _ = system('cls') 

    # for mac and linux(here, os.name is 'posix') 
    else: 
        _ = system('clear') 

Now you need to have a function that adds your word into a list then runs the clear function, then finally can pick a word at the end

Ironkey
  • 2,568
  • 1
  • 8
  • 30