I would like to be able to erase the last word printed onto the terminal. The last word should be the last string of text following a space. The following code simulates what I am trying to achieve. I want a function remove_last_word()
that will erase str
.
import random
words = ['apple', 'banana', 'cherry', 'dog', 'elephant', 'flower', 'guitar', 'house', 'island', 'jungle']
str = 'stop.\n'
for i in range(0, random.randint(0, 100)):
print(random.choice(words), end=' ')
print(str, end='')
remove_last_word()
Simply printing a carriage return won't work because there might be a newline at the end of the word. Moving the cursor up a line and printing a carriage return won't work because that will overwrite the previous words printed onto the terminal. I need a way to access the contents of the terminal so that I can find the start of the the last word.
I would like to be able to erase the last word printed onto the terminal. The last word should be the last string of text following a space. The following code simulates what I am trying to achieve. I want a function remove_last_word()
that will erase str
.
import random
words = ['apple', 'banana', 'cherry', 'dog', 'elephant', 'flower', 'guitar', 'house', 'island', 'jungle']
str = 'stop.\n'
for i in range(0, random.randint(0, 100)):
print(random.choice(words), end=' ')
print(str, end='')
remove_last_word()
Simply printing a carriage return won't work because there might be a newline at the end of the word. Moving the cursor up a line and printing a carriage return won't work because that will overwrite the previous words printed onto the terminal. I need a way to access the contents of the terminal so that I can find the start of the the last word.
Share Improve this question asked Feb 17 at 23:31 ERROR 404ERROR 404 111 bronze badge New contributor ERROR 404 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 3 |1 Answer
Reset to default 0I wasn't able to do this without curses, so here's a solution using curses:
import curses
import random
words = ['apple', 'banana', 'cherry', 'dog', 'elephant',
'flower', 'guitar', 'house', 'island', 'jungle']
def remove_last_word(stdscr):
# Clear the screen
stdscr.clear()
# Randomized words list using the vocabulary provided
picked_words = [random.choice(words) for _ in range(random.randint(1, 100))]
# Print selected words, separated by spaces
stdscr.addstr(' '.join(picked_words) + ' stop.\n')
stdscr.refresh()
# So we can observe the delete: wait for user input to remove the last word
stdscr.addstr("Press Enter to remove the last word...")
stdscr.refresh()
stdscr.getch() # Wait for user to press a key
# Remove the last printed word using curses
#-----------------------------------------------------
# 1.) Clear the screen
stdscr.clear()
if picked_words:
# 2.) Remove the last word
picked_words.pop()
# 3.) print it out again with one less word
stdscr.addstr(' '.join(picked_words) + ' stop.\n')
stdscr.refresh()
stdscr.getch() # Wait for another key press to exit
def main():
# Clearly, we are using curses to handle our console updates
curses.wrapper(remove_last_word)
if __name__ == "__main__":
main()
str
as a variable name, it's the name of a built-in type. – Barmar Commented Feb 17 at 23:37ncurses
library. – Barmar Commented Feb 17 at 23:39