Python job: Level 3 menu (Week 1 ),

Source: Internet
Author: User

Python job: Level 3 menu (Week 1 ),

I. Job 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

 

Ii. Third-level menu reading file content:

{'Beijing': {"Changping": {"shahe": ["oldboy", "test"], "tiantongyuan": ["chain home property ", "I love my family"]}, "Chaoyang": {"Wangjing": ["Benz", "momo"], "International trade": {"CICC", "HP "}, "Dongzhimen": {"Advent", "Feixin" },}, "Haidian" :{},}, 'shandong ': {"Dezhou ":{}, "Qingdao" :{}, "Jinan" :{}}, 'guangdong ': {"Dongguan" :{}, "Changshu" :{}, "Foshan ": {},},}

3. flowchart:

 

Iv. Code: (python3.6)

#! /Usr/bin/env python #-*-coding: UTF-8-*-# Author: Evenimport sysfile = open ("third-level menu text content", 'R ', encoding = 'utf-8') # Open Level 3 menu text file f = file. read () for line in f: # read the information of each row in the file file_str = str (f) # convert the information of each row to the string format data = eval (file_str) # converting a string to a dictionary format def list (): # define to read the key for I in data: print (I) def choose1 (choice1) in the dictionary: # define the first layer to select while choice1! = 'Q': # if the input is q, exit the program global one = choice1 # define the global variable one so that other functions can call if choice1 in data: for i2 in data [one]: print ("\ t", i2) # display the second-level information choose2 (input ("Please input second-level:") selected in level 3 menu :")) # Call the choose2 function and enter the second-level information break elif choice1 = 'B': # Enter B to exit the loop break else: error = input ("input error. Please enter the first layer again: ") # if error = 'q': # Enter q to exit the program sys. exit () else: # Otherwise, call this function choose1 (error) else: sys. exit () def choose2 (choice2): # define Select while choice2 on the second layer! = 'Q': global two = choice2 # defines the global variable two so that other functions can call if choice2 in data [one]: for i3 in data [one] [two]: print ("\ t", i3) # display choose3 (input ("Enter Layer 3:") in level 3 menu :")) # Call the choose3 function and enter the Level 3 Information break elif choice2 = 'B': # Enter B to return to the level 1 list () # Call the list function to display the first layer of Information choose1 (input ("Enter the first layer:") break else: error2 = input ("input error, Please re-enter the second layer :") # input error. re-enter if error2 = 'q': # Enter q to exit the program sys. exit () else: choose2 (error2) # Call this function else: sys. exit () def choose3 (choice3): # define the Layer 3 select while choice3! = 'Q': global three = choice3 # defines the global variable three so that other functions can call if choice3 in data [one] [two]: for i4 in data [one] [two] [three]: print ("\ t", i4) # display the layer-4 Information choose4 (input ("the last layer, return to the previous layer, press 'B', exit, press 'q") selected in the level-3 menu ")) # Call the choose4 function and prompt to exit or return break elif choice3 = 'B': # Enter B to exit choose1 (one) break else: error3 = input ("input error, please re-enter Layer 3: ") # input error. You can re-enter q and exit if error3 = 'q': sys. exit () else: choose3 (error3) # Call this function e Lse: sys. exit () def choose4 (choice4): # define the final exit while choice4! = 'Q': # input q to exit if choice4 = 'B': # input B to return to the previous choose2 (two) else: choose4 (input ("is the last layer, to return to the previous layer, press 'B'. to exit, press 'q'.) # declare to end else: sys. exit () print ("exit press 'Q', return to superior menu press 'B'") # declare function list () # Call the list function to display the first layer of Information choose1 (input ("Enter the first layer:") # main program to start running
View Code

 

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.