我有一个正在读取用户输入的while循环。但是我希望用户能够在任何时候打破while循环,这样他们就可以返回主菜单。在
目前,这是我唯一能做到的方法(如果用户在每一点后都输入了“--back”语句)。这个菜单代码从返回菜单.解析如果它被改变了。但在新菜单代码生效之前,while循环必须完全完成。有没有办法不用所有的if语句就可以做到这一点?在
menu是一个类,还有几个其他类。下面是主回路的一部分。在
一些菜单类:class Menu:
def __init__(self):
self.code = 'main'
self.codememory = []
def exit(self, input):
if input == '--exit':
sys.exit()
def back(self, input):
if input == '--back':
if 'main' in self.codememory:
print "should be going back"
print self.code
self.code = self.codememory.pop()
print self.code
def move(self, input):
if input == '--new':
self.codememory.append(self.code)
self.code = 'new'
if input == '--main':
self.codememory.append(self.code)
self.code = 'main'
if input == '--help':
self.codememory.append(self.code)
self.code = 'help'
def select(self, input):
if input in string.digits:
input = int(input)
if input == 1:
self.codememory.append(self.code)
self.code = 'new'
if input == 2:
self.codememory.append(self.code)
self.code = 'test'
def header(self, title):
os.system('clear')
print "-------------------"
print title.upper()
print "-------------------"
print ""
def parse(self, input):
self.exit(input)
self.back(input)
if self.code == 'new':
return input.lower().decode('utf-8')
############################
menu = Menu()
while 1:
...
while menu.code == 'new':
menu.header('save word')
key = menu.parse(raw_input("Norwegain: "))
while key in dict.keys:
print "word already Saved in Dictionary"
key = menu.parse(raw_input("Norwegain: "))
if menu.code == 'new':
val = menu.parse(raw_input("English: "))
if menu.code == 'new':
syn = menu.parse(raw_input("Synonym: "))
if menu.code == 'new':
ant = menu.parse(raw_input("Antonym: "))
while menu.code == 'main':
...