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()
        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,它会重复 main 功能。谢谢你的帮助。

4

4 回答 4

5

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

资源

name = 'Studboy'
while True:
    print("\nMenu\n(V)iew High Scores\n(P)lay Game\n(S)et Game Limits\n(Q)uit")
    choice = raw_input(">>> ").lower().rstrip()
    if choice=="q":
        break
    elif choice=="v":
        highScore()
    elif choice=="s":
        setLimit()
    elif choice=="p":
        game()
    else:
        print("Invalid choice, please choose again\n")

print("Thank you for playing,",name)
print(".")
于 2014-08-24T00:17:03.023 回答
1
 def Menu:
     while True:
          print("1. Create Record\n2. View Record\n3. Update Record\n4. Delete Record\n5. Search Record\n6. Exit")
          MenuChoice=int(input("Enter your choice: "))
          Menu=[CreateRecord,ViewRecord,UpdateRecord,DeleteRecord,SearchRecord,Exit]
          Menu[MenuChoice-1]()
于 2019-05-25T05:12:36.513 回答
0

在进入循环之前,您只会从用户那里获得一次输入。因此,如果他们第一次输入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时退出而言,它的工作方式与我预期的一样。

它已经从您的版本中进行了一些调整,以便在Python 2.7(raw_inputvs. input)中工作,并且从 the 中删除了nameand引用,因此它可以编译(我假设这些是在您的代码中的其他地方定义的)。我还定义了函数的虚拟版本,这样它就可以编译并反映调用行为,这就是这里要检查的内容。endprintgame

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,")
于 2014-08-24T00:08:00.890 回答
0

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

  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()
于 2019-06-13T19:31:31.153 回答