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
4
  • take a look at the curses module Commented Jan 14, 2020 at 15:02
  • what output are you expecting Commented Jan 14, 2020 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 Commented Jan 14, 2020 at 15:34
  • Does this answer your question? Delete the last input row in Python Commented Feb 4, 2024 at 0:59

2 Answers 2

3

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This is exactly what I was searching for.
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

1 Comment

I think clear clears the whole screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.