Python菜单驱动编程
在菜单驱动的编程中,怎么写一个退出功能,才能让它在只按一次的时候就结束程序呢?
这是我的代码,如果可以的话请帮我修改一下:
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”可以退出。但是如果按了其他功能,再返回主菜单后按“q”,它又会重复执行主功能。
谢谢你的帮助。
4 个回答
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()
0
你只在进入循环之前让用户输入一次。所以如果他们第一次输入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()
然后在进入循环之前调用这个函数,循环的最后一步也可以调用它,这样就能确保每次循环都检查用户的输入。
根据提问者的评论进行的编辑:
下面的代码实现了我提到的提取功能,当输入q时,它会按预期退出。
这段代码稍微修改了一下,以适应Python 2.7(使用raw_input
而不是input
),同时也去掉了print
中的name
和end
引用,以便能正常编译(我假设这些在你的代码中是定义过的)。我还定义了一些像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,")
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]()
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
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(".")