Program: Level Three Menu
Requirements:
- Print provincial, city, and county level three menus
- Can return to the upper level
- Can exit the program at any time
1menu = {2 'Beijing':{3 'Haidian':{4 'Five crossing':{5 'Soho':{},6 'NetEase':{},7 'Google':{}8 },9 'Zhongguancun':{Ten 'Iqiyi Art':{}, One 'Autohome':{}, A 'Youku':{}, - }, - 'on the ground':{ the 'Baidu':{}, - }, - }, - 'changping':{ + 'Shahe':{ - 'old boy':{}, + 'Beihang University':{}, A }, at 'Tin Tong Court':{}, - 'Huilongguan':{}, - }, - 'Chaoyang':{}, - 'Dongcheng':{}, - }, in 'Shanghai':{ - 'Minhang':{ to "People's square":{ + 'Fried Chicken Shop':{} - } the }, * 'Zhabei':{ $ 'Train Warfare':{Panax Notoginseng 'Ctrip':{} - } the }, + 'Pudong':{}, A }, the 'Shandong':{}, + } - $Exit_flag = True#set a pointer that jumps out of the entire loop $ - whileExit_flag:#If you want to keep in this menu, you have to use the dead loop directly; - #If there are several loops inside, then the existing knowledge can only set an outside variable to set the exit condition the forIinchMenu#show the current level of content - Print(i)Wuyi theChoice1 = input ('Please enter the province, exit please enter Q') - Wu ifChoice1inchMenu: - #This is the first floor of the province . About whileExit_flag: $ forI2inchMenu[choice1]: - Print(I2) -Choice2 = input ('Please enter the area, B is the previous layer, Q is the exit') - ifChoice2inchMenu[choice1]: A #This is the first floor of the district . + whileExit_flag: the fori3inchMenu[choice1][choice2]: - Print(i3) $Choice3 = input ('Please enter a location, B is the previous layer, Q is the exit') the ifChoice3inchMenu[choice1][choice2]: the #This is the bottom . the whileExit_flag: the ifi3inchMenu[choice1][choice2][choice3]: - Print(i3) in Print("This is the last floor .") theChoice4 = input ('Select B, or Q') the ifChoice4 = ="b": About Break the elifChoice4 = ="Q": theExit_flag =False the Break #here must remember break, so exit_flag can take effect, jump out of the entire cycle + Else: - Print('your input was wrong.') the Break #You must remember to quit here .Bayi elifChoice3 = ="b": the Break the elifChoice3 = ="Q": -Exit_flag =False - Break #here must remember break, so exit_flag can take effect, jump out of the entire cycle the Else: the Print('your input was wrong.') the Break #You must remember to quit here . the elifChoice2 = ="b":#elif can be used together so that multiple layers of nested if are avoided. - Break the elifChoice2 = ="Q": theExit_flag =False the Break #here must remember break, so exit_flag can take effect, jump out of the entire cycle94 Else: the Print('your input was wrong.') the Break #You must remember to quit here . the elifChoice1 = ="b":98 Break About elifChoice1 = ="Q": -Exit_flag =False101 Break #here must remember break, so exit_flag can take effect, jump out of the entire cycle102 Else:103 Print('your input was wrong.')104 Break #You must remember to quit here . the 106 Break
Written like this feeling is still quite low, the following more than 20 lines of code is copied.
Python program 3--level three menu program