data structure: Menu = {' Beijing ': {' Haidian ': {' Five crossings ': {' Soho ': {}, ' NetEase ': {}, ' Google ': {}}, ' Zhongguancun ': {' Iqiyi ': {}, ' Autohome ': {}, ' Youku ': {},}, ' on the ground ': {' Baidu ': {},},}, ' Changping ': { ' Shahe ': {' old boy ': {}, ' Beihang ': {},}, ' Tian Tong Yuan ': {}, ' Huilongguan ': {}, }, ' Chaoyang ': {}, ' Dongcheng ': {},}, ' Shanghai ': {' Minhang ': {' People's Square ': {' Fried chicken Shop ': {} }}, ' Zhabei ': {' rail war ': {' Ctrip ': {}}}, ' Pudong ': {},}, ' Shandong ': { },} Requirements: You can choose to go to each submenu from any level back to the previous layer you can exit the program from any level required new knowledge points: List, dictionary
Reference Code is as follows:
# coding:utf-8
#!/usr/bin/env python3
Menu = {
' Beijing ': {
' Haidian ': {
' five crossings ': {
' Soho ': {},
' NetEase ': {},
' Google ': {}
},
' Zhongguancun ': { ' Iqiyi art ': {},
' Autohome ': {},
' Youku ': {},
},
' Land ': {
' Baidu ': {},
},
},
' Changping ': {
' Shahe ': {
' Old boy ': {},
' Beihang ': {},
},
' Tian Tong Yuan ': {},
' Huilongguan ': {},
} ,
' Chaoyang ': {},
' Dongcheng ': {},
},
' Shanghai ': {
' Minhang ': {
' People's Square ': {
' Fried Chicken Shop ': {}
}
},
' Zhabei ': {
' train war ': {
' Ctrip ': {}
}
},
' Pudong ': {},
},
' Shandong ': {},
}
#递归方式实现
def show_menu (CH):
For s in CH:
Print (s)
Print (' Return/exit ')
p = input (' You select Yes ')
if p = = ' exit ':
Exit ()
elif p = = ' return ':
Return
Else
If p in Ch:
Show_menu (Ch[p])
Show_menu (CH)
Show_menu (Menu)
Three-level menu Python notation (recursive notation)