How should I write this so I could constantly move between “menus”.
#!/bin/env python
import os
class Menu:
def __init__(self):
self.menu = '1'
def Main(self):
os.system('clear')
print "main menu"
test = raw_input()
if test == '2':
self.menu = '2'
def Sub(self):
os.system('clear')
print "sub menu"
test = raw_input()
if test == '1':
self.menu = '1'
menu = Menu()
while menu.menu == '1':
menu.Main()
while menu.menu == '2':
menu.Sub()
At the moment I can swap once. ie I start with menu.Main(), enter ‘2’ and menu.Sub() is shown. But then when I enter ‘1’ the program quits. why does it not go back to showing menu menu.Main() ? Any thoughts welcome!
EDIT:
just needed to put them in a main while loop
The first while loop runs, and when you enter ‘2’, finishes. Therefore, the second while loop will begin to loop.
In the second while loop, you enter ‘1’, which causes the second while loop to finish (because menu.menu is now == ‘1’). Thus, the program finishes.
Instead, you’ll probably want one value for menu (that is neither ‘1’ nor ‘2’) to act as the exit state. For example, ‘E’. Then, you can replace your two while loops with the following:
The “Do” method will handle the menu state if it’s 1 or 2.
You will still need to make it so that you can actually get to the ‘E’ case. I’ll leave that as a task for you to finish.