Python 2: Level 3 menu,

Source: Internet
Author: User

Python 2: Level 3 menu,

Level 3 menu requirements:

1. Run the program to output the first menu

2. Select a level-1 menu and output level-2 menus. Similarly, level-3 menus are output.

3. Save menu data in a file

4. Ask the user to select whether to exit

5. The function of returning the top menu

 

Program code:

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-

Menu = open('menu.txt ', encoding = 'utf-8') extract all place names at intervals from the menu.txt File
For j in menu. readlines (): # assign values to the variables corresponding to all place names.
Region, province_db, province_hb, city_hlj, city_jl, city_hb, city_sx, area_heb ,\
Area_jms, area_cc, area_jl, area_bd, area_sjz, area_ty, area_dt = j. split ('$ ')

Flag = True # Set the program termination variable
While flag: # enter the first-level menu to display the first-level menu content, waiting for the user to enter
Print ("select region: \ n (enter q to exit )")
Print (region)
Num = input ("----> ")
If num = '1': # judge user input. if the user selects the first menu, go to the following loop.
While flag: # enter the level-2 menu, display the level-2 menu content, waiting for the user to enter
Print ("select the province: \ n (enter B to return to the upper menu and enter q to exit )")
Print (province_db)
Num = input ("----> ")
If num = '1': # judge user input. if the user selects the first menu, go to the following loop.
While flag: # enter the Level 3 menu, display Level 3 menu content, waiting for user input
Print ("select the city: \ n (enter B to return to the upper menu and enter q to exit )")
Print (city_hlj)
Num = input ("----> ")
If num = '1': # determine whether the user selects the first menu and enter the following Loop
While flag: # display the content in level 3 menu
Print ('your City has the following districts: \ n % s' % (area_heb ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B': # if the user enters "B", the system jumps out of the current loop and returns to the upper menu.
Break
Elif num = 'q': # If you enter "q", set the program termination variable to False to exit all loops and terminate the program.
Flag = False
Else: # other characters entered by the user. If the input is incorrect, enter another character.
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_jms ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ("select the city: \ n (enter B to return to the upper menu and enter q to exit )")
Print (city_jl)
Num = input ("----> ")
If num = '1 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_cc ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_jl ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')

Elif num = '2 ':
While flag:
Print ("select the province: \ n (enter B to return to the upper menu and enter q to exit )")
Print (province_hb)
Num = input ("----> ")
If num = '1 ':
While flag:
Print ("select the city: \ n (enter B to return to the upper menu and enter q to exit )")
Print (city_hb)
Num = input ("----> ")
If num = '1 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_bd ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_sjz ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ("select the city: \ n (enter B to return to the upper menu and enter q to exit )")
Print (city_sx)
Num = input ("----> ")
If num = '1 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_ty ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = '2 ':
While flag:
Print ('your City has the following districts: \ n % s' % (area_dt ))
Print ('enter B and return to the upper menu. Enter q to exit ')
Num = input ("----> ")
If num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'B ':
Break
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')
Elif num = 'q ':
Flag = False
Else:
Print ('input is incorrect. Please input \ n' again ')

Menu. close ()


Menu file:
1. Northeast Region 2, North China region $1, Heilongjiang Province 2, Jilin province $1, Hebei 2, Shanxi $1, Harbin 2, Jiamusi city $1, Changchun 2, Jilin City $1, baoding City 2, Shijiazhuang city $1, Taiyuan City 2, Datong City $1, daoli District 2, Nangang district $1, Qianjin District 2, Xiangyang district $1, nanguan District 2, Kuan Cheng District $1, chuanying District 2, longtan district $1, jingxiu District 2, lianchi district $1, Xinhua District 2, Chang 'an district $1, xiaodian District 2, Yingze district $1, South Suburb 2, xinrong District

Important Position of the program:
1,
Menu = open('menu.txt ', encoding = 'utf-8') # The decoder must be specified because the menu file contains Chinese characters.
For j in menu. readlines (): # Use "$" in the file as a flag to split the file and assign the file content to the corresponding variables.
Region, province_db, province_hb, city_hlj, city_jl, city_hb, city_sx, area_heb ,\
Area_jms, area_cc, area_jl, area_bd, area_sjz, area_ty, area_dt = j. split ('$ ')

2,
If num = 'B': # if you enter B
Break # Jump out of this loop
Elif num = 'q': # If you enter q
Flag = False # Set flag to Flase, so that all while loop values become False and the program is terminated.


Program running error explanation:

This error is a decoding error when retrieving data from the file. Because the file contains Chinese characters, you need to add the encoding = 'utf-8' parameter when reading the file.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.