I have a while
loop that is reading user input. But I want the user to be able to break the while
loop at any point, so they can go back to a main menu.
At the moment this is the only way I can do it (if statements after every point that the user may have typed '--back'. The menu.code is returned from the menu.parse function if it is changed. But then the while loop has to completely finish before it's new menu code takes effect. Is there a way of doing this without all the if statements?
menu
is a class, and there are a couple of others. Below is a section from the main loop.
Some of the menu class:
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':
...