Python菜单驱动编程

Python菜单驱动编程,python,menu,python-3.1,Python,Menu,Python 3.1,对于菜单驱动的编程,如何编写Quit函数的最佳方式,以便Quit只在一个响应中终止程序 这是我的代码,如果可能,请编辑: print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit") choose=input(">>> ") choice=choose.lower() while choice!="q": if choice=="v": highScore()

对于菜单驱动的编程,如何编写Quit函数的最佳方式,以便Quit只在一个响应中终止程序

这是我的代码,如果可能,请编辑:

print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
choose=input(">>> ")
choice=choose.lower()
while choice!="q":
    if choice=="v":
        highScore()
        main()
    elif choice=="s":
        setLimit()
        main()
    elif choice=="p":
        game()
        main()
    else:
        print("Invalid choice, please choose again")
        print("\n")
print("Thank you for playing,",name,end="")
print(".")
当程序第一次执行并按“q”键时,它退出。但在按下另一个功能后,返回main并按下q,它会重复主功能。
感谢您的帮助。

在进入循环之前,您只能从用户那里获得一次输入。因此,如果他们第一次进入q,那么它将退出。但是,如果他们不这样做,它将继续跟踪输入的内容,因为它不等于q,因此不会跳出循环

您可以将此代码分解为一个函数:

print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
choose=input(">>> ")
choice=choose.lower()
然后在进入循环之前调用这两个函数,然后就像循环在返回之前做的最后一件事一样

根据OP的评论进行编辑:

下面的代码实现了我前面提到的分解,在键入q时,它的工作方式与我预期的一样

它从您的版本中进行了一些调整,以便在Python2.7中工作(
raw_input
vs.
input
),并且
名称和
结束
引用从
打印
中删除,因此它可以编译(我假设它们是在代码的其他地方定义的)。我还定义了函数的虚拟版本,比如
game
,这样它就可以编译并反映调用行为,这就是本文要研究的

def getChoice():
    print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
    choose=raw_input(">>> ")
    choice=choose.lower()

    return choice

def game():
    print "game"

def highScore():
    print "highScore"

def main():
    print "main"

def setLimit():
    print "setLimit"


choice = getChoice()

while choice!="q":
    if choice=="v":
        highScore()
        main()
    elif choice=="s":
        setLimit()
        main()
    elif choice=="p":
        game()
        main()
    else:
        print("Invalid choice, please choose again")
        print("\n")

    choice = getChoice()

print("Thank you for playing,")

在进入循环之前,您只能从用户处获得一次输入。因此,如果他们第一次进入q,那么它将退出。但是,如果他们不这样做,它将继续跟踪输入的内容,因为它不等于q,因此不会跳出循环

您可以将此代码分解为一个函数:

print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
choose=input(">>> ")
choice=choose.lower()
然后在进入循环之前调用这两个函数,然后就像循环在返回之前做的最后一件事一样

根据OP的评论进行编辑:

下面的代码实现了我前面提到的分解,在键入q时,它的工作方式与我预期的一样

它从您的版本中进行了一些调整,以便在Python2.7中工作(
raw_input
vs.
input
),并且
名称和
结束
引用从
打印
中删除,因此它可以编译(我假设它们是在代码的其他地方定义的)。我还定义了函数的虚拟版本,比如
game
,这样它就可以编译并反映调用行为,这就是本文要研究的

def getChoice():
    print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
    choose=raw_input(">>> ")
    choice=choose.lower()

    return choice

def game():
    print "game"

def highScore():
    print "highScore"

def main():
    print "main"

def setLimit():
    print "setLimit"


choice = getChoice()

while choice!="q":
    if choice=="v":
        highScore()
        main()
    elif choice=="s":
        setLimit()
        main()
    elif choice=="p":
        game()
        main()
    else:
        print("Invalid choice, please choose again")
        print("\n")

    choice = getChoice()

print("Thank you for playing,")

将菜单和解析放在一个循环中。当用户想要退出时,使用
break
中断循环

来源
将菜单和解析放在一个循环中。当用户想要退出时,使用
break
中断循环

来源 这是一个用于矩阵加减的菜单驱动程序

这是一个用于矩阵加减的菜单驱动程序

  def getchoice():
        print('\n What do you want to perform:\n 1.Addition\n 2. Subtraction')
        print('Choose between option 1,2 and 3')
        cho = int(input('Enter your choice : '))
        return cho


    m = int(input('Enter the Number of row    : '))
    n = int(input('Enter the number of column : '))
    matrix1 = []
    matrix2 = []

    print('Enter Value for 1st Matrix : ')
    for i in range(m):
        a = []
        for j in range(n):
            a.append(int(input()))
        matrix1.append(a)
    print('Enter Value for 2nd Matrix : ')
    for i in range(m):
        a = []
        for j in range(n):
            a.append(int(input()))
        matrix2.append(a)
    choice = getchoice()
    while choice != 3:
        matrix3 = []
        if choice == 1:
            for i in range(m):
                a = []
                for j in range(n):
                    a.append(matrix1[i][j] + matrix2[i][j])
                matrix3.append(a)
            for r in matrix3:
                print(*r)
        elif choice == 2:
            for i in range(m):
                a = []
                for j in range(n):
                    a.append(matrix1[i][j] - matrix2[i][j])
                matrix3.append(a)
            for r in matrix3:
                print(*r)
        else:
            print('Invalid Coice.Please Choose again.')

        choice = getchoice()