-3

I'm doing an application for fun in python. One of the things i want to do, is print out to terminal a list of option to choose from by navigating up and down using arrow keys (If it's possible). for example: Hey, please choose one of the following options:

1-XXXXX

2-YYYYY

3-ZZZZZ

and the user chooses the one highlighted. and how do i bind what the customer chose to a variable? Thanks!

  • choice = input('Choose an option:') – ksbg Apr 11 '18 at 08:40
  • 1
    Possible duplicate of [Python: user input and commandline arguments](https://stackoverflow.com/questions/70797/python-user-input-and-commandline-arguments) – ksbg Apr 11 '18 at 08:41

1 Answers1

1

If you don't want to use "input" and have the user to enter a number, but want him to interact with your application, there are some nice libraries to do that (it requires some code, however).

One of them is Asciimatics (see https://github.com/peterbrittain/asciimatics) that also provides several widgets to use in a terminal. The widget you could be interested in is the ListBox: http://asciimatics.readthedocs.io/en/stable/asciimatics.html#asciimatics.widgets.ListBox

However, as I said, this requires more code than simply writing

choice = None
while choice not in [1, 2, 3]:
   try:    
      choice = int(input('1, 2 or 3? '))
   except ValueError:
      pass
Guybrush
  • 2,680
  • 1
  • 10
  • 17