Is there a Python function that would let a user pick from a menu of options in the terminal?
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.
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 :)