问题
How do I make a simple Menu/Directory using Python? I would like to have letters that the user would press to do tasks, and when they enter the letter after the prompt, the task is done... for example:
A. Create Username
B. Edit Username
C. Exit
Choice:
And then all the user has to do is enter one of the letters after the prompt.
回答1:
A (very) basic approach would be something like this:
print "A. Create Username"
print "B. Edit Username"
input = raw_input("Enter your choice")
if input == "A":
print "A was given"
if input == "B":
print "B was given"
回答2:
A very basic version:
def foo():
print "Creating username..."
def bar():
print "Editing username..."
while True:
print "A. Create Username"
print "B. Edit Username"
print "C. Exit"
choice = raw_input()
if choice.lower() == 'a':
foo()
elif choice.lower() == 'b':
bar()
elif choice.lower() == 'c':
break
else:
print "Invalid choice"
Accepts upper- and lower-case letters as choice.
回答3:
Console Menu Generator in Python
Have a read and post back with your efforts as stated by dm03514
来源:https://stackoverflow.com/questions/17454882/how-to-create-a-menu-or-directory