Skip to content
Sign upLog in
← Back to Community

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

Profile icon
CrapCodes

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.

Answered by ArchieMaclean [earned 5 cycles]
View Answer
Voters
Profile icon
CrapCodes
Comments
hotnewtop
Profile icon
ArchieMaclean

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 :)

Profile icon
CrapCodes

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

Profile icon
ArchieMaclean

@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.

Profile icon
CrapCodes

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

Profile icon
CrapCodes

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

Profile icon
ArchieMaclean

@CrapCodes
Glad to be of help :)