How to create a menu or directory? [closed]

耗尽温柔 提交于 2020-01-06 02:22:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!