python console menu level looping

Daiyue Weng daiyueweng at gmail.com
Sun Sep 24 16:41:30 EDT 2017


Hi, I tried to make a menu using print statements in Python 3. The code is
as follows,

print('insert data into: ')
data_insert_method = ['new collection', 'existing collection']
for index, elem in enumerate(data_insert_method):
print(index, '-', elem)

while 1:
how_to_insert = input('Choose a method for inserting data: ')
if how_to_insert:
try:
how_to_insert = int(how_to_insert)
how_to_insert = data_insert_method[how_to_insert]
break
except ValueError:
print('Please type in an integer corresponding to the data insert method')
continue
except IndexError:
print('insert method index out of range')
continue

if how_to_insert == 'new collection':

print('Set up collection name : ')
db_name_setup = ['manual setup', 'return to the main menu']
for index, elem in enumerate(db_name_setup):
print(index, '-', elem)

while 1:
how_to_setup = input('Choose a method for setting up collection: ')
if how_to_setup:
try:
how_to_setup = int(how_to_setup)
how_to_setup = db_name_setup[how_to_setup]
break
except ValueError:
print('Please type in an integer corresponding to the collection setup
method')
continue
except IndexError:
print('collection setup method index out of range')
continue

if how_to_setup == 'manual setup':
print('Please type in collection name: ')
elif how_to_setup == 'return to main menu':
print('return to main menu')

It is a 2-level menu, on the 2nd level menu 'new collection', there is an
option - 'return to the main menu', I am wondering how to loop back to the
1st level menu, i.e. ['new collection', 'existing collection'],
whenever 'return
to the main menu' is selected.

cheers



More information about the Python-list mailing list