Is there a Python function that would let a user pick from a menu of options in the terminal?
CrapCodes (0)

I'm doing a Python calculator project and I was wondering if there was a way to let a user chose from a list of potential "input" items, kind of like an extension of the input function.

You are viewing a single comment. View All
ArchieMaclean (731)

There is no built in function.

You would probably want to do something like this, to code it yourself:

def choose_from_list(list):    # a function called choose_from_list gets users choice from list
  print("Choose from the following:\n")
  print("\n".join(list))    # prints the list separated by \n (newline character)
  ans = input()              # gets input
  while ans not in list:    # if the user typed in something not in the list
    print("That's not an option.")
    ans = input("Choose again please: ")
  return ans    # return what the user chose

my_list = ["apple","banana","pear"]
user_choice = choose_from_list(my_list)

Of course, you can change this to fit whatever you want to do.

Please mark this as the answer if it solved your problem :)

CrapCodes (0)

@ArchieMaclean I am not cultured enough to fully understand how that works but I'll try it, thanks.

ArchieMaclean (731)

@CrapCodes You can just use the function if you want, without understanding it.

Can you mark it as the answer, so people know your issue has been solved? Thanks.

CrapCodes (0)

@ArchieMaclean Sorry, I have't been on or coded anything in a while. Just got on to learn of and implement in now actually.

CrapCodes (0)

@ArchieMaclean Oh i understand it now, thanks for the help.