0

I have a menu as shown in the picture below.

menu

I'm waiting for an input from the user to choose one of the three. But when user enters something, for example 2, it gets displayed in the console and it waits for the user to press enter. But I want it such that when the user hits a number, the menu disappears instantly. How do I do that?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Does this answer your question? [Python read a single character from the user](https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user) – kojiro Dec 17 '19 at 03:12
  • Perhaps [getpass](https://docs.python.org/3.8/library/getpass.html#getpass.getpass) can help you. –  Dec 17 '19 at 03:49
  • Does this answer your question? [raw\_input in python without pressing enter](https://stackoverflow.com/questions/3523174/raw-input-in-python-without-pressing-enter) – ParkerD Dec 17 '19 at 05:29

2 Answers2

0

You should clear the screen, something like this should work:

import os
os.system('cls' if os.name == 'nt' else 'clear')
marcos
  • 4,473
  • 1
  • 10
  • 24
0

You can use the keyboard package. Install it by running pip install keyboard in your terminal and import it into your code with import keyboard. To detect the user input:

if keyboard.is_pressed('1'): 
    # do searach stuff
elif keyboard.is_pressed('2'):
    # show most used words
elif keyboard.is_pressed('3'):
    # Goodbye, exit
else:
    # (a) clear the screen (e.g. os.system('cls' if os.name == 'nt' else 'clear'))   
    # (b) reprint the menu
    # (c) print "Bad input, try again"

You might find this answer helpful as well.

Ali Atiia
  • 139
  • 7