Python implements three-level menu operations

Source: Internet
Author: User

#!/usr/bin/env Python3
# Encoding:utf-8
# __author__: Chenwei
# date:2017-05-25
# blog:http://www.cnblogs.com/weiandlu/

The JSON data file format is as follows:
{
' Beijing ': {
' Haidian ': {
' Five crossing ': {
' Soho ': {},
' NetEase ': {},
' Google ': {}
},
' Zhongguancun ': {
' Iqiyi art ': {},
' Autohome ': {},
' Youku ': {},
},
' On the ground ': {
' Baidu ': {},
},
},
' Changping ': {
' Shahe ': {
' Higher Education Park ': {},
' Beihang University ': {},
},
' Tian Tong Yuan ': {},
' Huilongguan ': {},
},
' Chaoyang ': {},
' Dongcheng ': {},
},
' Shanghai ': {
' Minhang ': {
"People's Square": {
' Fried Chicken Shop ': {}
}
},
' Zhabei ': {
' Train war ': {
' Ctrip ': {}
}
},
' Pudong ': {},
},
' Shandong ': {},
}
‘‘‘
Import JSON
Def read_data ():
With open ("Administrative_division.json", "R", encoding= "UTF8") as F_read:
Data=json.load (F_read)
Return data

def write_data (data):
With open ("Administrative_division.json", "W", encoding= "UTF8") as F_write:
Json.dump (Data,f_write)

data = Read_data ()
Current_layer = Read_data ()
Last_layer = [] #在菜单层层深入时记录上一层菜单
Layer_count = {' layer ': 0, ' key ': []} #= the dictionary's layer value is increased by 1 per layer, and the city name selected at each level append to the list of keys
Num_to_menu = {} #将菜单列表打印的序列号与城市名成进行对应
While True:
Print ("Welcome to Level three menu system". Center (50, "#"))
For i,v in Enumerate (current_layer,1):
Print ("%s\t%s\t"% (i,v), end= ")
Num_to_menu[i] = V
Print ()
Choice=input ("Please select the letter before option: \na: Add, D: Delete, M: Modify, B: Return, Q: Exit:"). Strip ()
If Choice.isdigit () and int (choice) in Num_to_menu:
Last_layer.append (Current_layer)
Current_layer = Current_layer[num_to_menu[int (choice)]
layer_count[' layer '] + = 1
layer_count[' key '].append (num_to_menu[int (choice)))
Write_data (data)
elif Choice = = ' A ':
City=input ("Please enter city name!:"). Strip ()
If layer_count[' layer '] = = 1:
If not data[layer_count[' key '][0]]:
data[layer_count[' key '][0]].update ({city:{}})
elif layer_count[' layer '] = = 2:
If not data[layer_count[' key '][0]][layer_count[' key '][1]]:
data[layer_count[' key '][0]][layer_count[' key '][1]].update ({city:{}})
elif layer_count[' layer '] = = 3:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]]:
data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]].update ({city:{}})
elif layer_count[' layer '] = = 4:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]]:
data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]].update ({city: {}})
Else
Pass
Current_layer.update ({city:{}})
Write_data (data)
elif Choice = = ' d ':
City_num=int (Input ("Please enter delete City serial number!") "). Strip ())
layer_count[' layer '] + = 1
layer_count[' key '].append (Num_to_menu[city_num])
If layer_count[' layer '] = = 1:
If not data[layer_count[' key '][0]]:
Del data[layer_count[' key '][0]]
elif layer_count[' layer '] = = 2:
If not data[layer_count[' key '][0]][layer_count[' key '][1]]:
Del data[layer_count[' key '][0]][layer_count[' key '][1]
elif layer_count[' layer '] = = 3:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]]:
Del data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]
elif layer_count[' layer '] = = 4:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]]:
Del data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]
Else
Pass
Write_data (data)
elif Choice = = ' m ':
City_num=int (Input ("Enter the city number to be modified!") "). Strip ())
New_name=input ("Please enter a new city name!") "). Strip ()
layer_count[' layer '] + = 1
layer_count[' key '].append (Num_to_menu[city_num])
If layer_count[' layer '] = = 1:
If not data[layer_count[' key '][0]]:
data[new_name]=data[layer_count[' key '][0]]
Del data[layer_count[' key '][0]]
elif layer_count[' layer '] = = 2:
If not data[layer_count[' key '][0]][layer_count[' key '][1]]:
data[layer_count[' key '][0]][layer_count[' key '][new_name]]=data[layer_count[' key '][0]][layer_count[' key '][1]
Del data[layer_count[' key '][0]][layer_count[' key '][1]
elif layer_count[' layer '] = = 3:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]]:
data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][new_name]]=data[layer_count[' key '][0]][ layer_count[' key '][1]][layer_count[' key '][2]
Del data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]
elif layer_count[' layer '] = = 4:
If not data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]]:
data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][new_name]]=data[ layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]
Del data[layer_count[' key '][0]][layer_count[' key '][1]][layer_count[' key '][2]][layer_count[' key '][3]
Else
Pass
Write_data (data)
elif Choice = = ' B ':
If Current_layer! = data:
Current_layer = Last_layer[-1]
Last_layer.pop ()
layer_count[' layer ']-= 1
layer_count[' key '].pop ()
Print (Layer_count)
Continue
Else
Print ("\033[31;1myou has reach to the top layer!\033[;0m")
elif Choice = = ' Q ':
Exit (1)
Else
Print ("\033[1;31;40m Please choose according to the prompts!") \033[;0m ")

Python implements three-level menu operations

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.