Level 3 menu (rookie version)

need:

  1. tertiary menu
  2. Can be selected in turn to enter each submenu

Readme:

  1. Menu selection order: province (level 1) --> subordinate city (level 2) --> subordinate county (level 3)

  2. You can directly exit the program at all levels of the menu, and at the city or county level, you can press b to return to the first level menu

  3. It is completely done by myself, and the implementation method is different from that on the Internet. I feel that my method is relatively stupid.

flow chart:

   

 

Code:

#-*- coding:utf-8 -*-
#Author:Sam

list = {'Guangdong Province':
            {'Jieyang City':
                 ['Jiedong County', 'Huilai County'],
             'Qingyuan City':
                 ['Yangshan County', 'Lianshan County']},
        'Hunan Province':
            {'Zhuzhou City':
                 ['You County', 'Chaling County'],
             'Changsha City':
                 ['Changsha County', 'Liuyang City']}
        }
province = [] #province list
city ​​= [] #City list
county = [] #County list

while True:
    for index,i in enumerate(list.keys()):
        print(index,i) #List the subscripts and provinces together, so that the menu can be selected with numbers later
        province.append(i) #Add province list

    user_choice_province = input("Please select a province, press b to return, press q to exit >>>:")
    if user_choice_province.isdigit(): #Determine whether the input is a number
        user_choice_province = int(user_choice_province) #Convert input to integer
        if user_choice_province >= 0 and user_choice_province < len(list): #Determine whether the selection is correct
            choice_province = province[user_choice_province] #The selected province generates a variable
            city_list = list.get(choice_province) #Use the dictionary attribute to get the subordinate cities of the corresponding province and generate a new dictionary

            for index, i in enumerate(city_list): #Using the new dictionary above, list the subscripts and subordinate cities for selection
                print(index, i)
                city.append(i) #Generate a city list for the convenience of the following operations
            user_choice_city = input("Please select the city, press b to return, press q to exit >>>>:")
            if user_choice_city.isdigit():
                user_choice_city = int(user_choice_city)
                if user_choice_city >=0 and user_choice_city < len(city):
                    choice_city = city[user_choice_city]
                    county_list = city_list.get(choice_city)

                    for index,i in enumerate(county_list):
                        print(index,i)
                    user_choice_county = input("Please select the county, press b to return, press q to exit >>>>:")
                    if user_choice_county.isdigit():
                        user_choice_county = int(user_choice_county)
                        if user_choice_county >=0 and user_choice_county <len(county_list):
                            print(county_list[user_choice_county])

                        elif user_choice_county == 'b':
                            break
                        elif user_choice_county == 'q':
                            exit("exit")
                        else:
                            print("input error")
                elif user_choice_city == 'b':
                    break
                elif user_choice_city == 'q':
                    exit("exit")
                else:
                    print("input error")
        elif user_choice_province == 'b':
            break
        elif user_choice_province == 'q':
            exit("exit")
        else:
            print("input error")
    else:
        exit("exit")

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781828&siteId=291194637