Python 三级菜单 增强版

需要实现的功能是:
三级菜单
1.从文本内读出选项
2.查询每一级的选项,并能对选项进行增/删/改功能
3.每一级可以退出程序或者返回上一层

2018-5-14 更新内容

思路

Python 三级菜单 增强版

实现过程中的BUG及解决方案:

1. windowsError [Error 32]

说明:进程无法访问文件,因为另一个程序正在使用此文件。

解决:在对文件进行操作之前 进行关闭文件操作

代码实现:

    FileRead.close()
    FileWrite.close()
    os.remove('city.txt')
    os.renames('city_backup.txt','city.txt')

2 .ValueError: I/O operation on closed file

说明:操作的文件已经关闭

解决:在对文件进行读取内容或者写入内容之前打开文件

代码实现:

    os.remove('city.txt')
    os.renames('city_backup.txt','city.txt')
    FileRead = io.open('city.txt', 'r', encoding='utf8')
    FileWrite = open('city_backup.txt', 'w+')

3.TypeError: expected a string or other character buffer object

说明:数据类型与预定的数据类型不一致

解决:使用eval函数讲读取到的字符串转为字典,写入文件的时候用str函数将字典转为字符串

代码实现:

menu = eval(FileRead.read())
menucopy = str(menu)
FileWrite.write(menucopy)
项目代码

 #coding=utf-8
 #__author:Administrator
 #__time:2018/5/14 17:34
 #__file_name:22222
 import io
 import os
 Operation =['a','b','c','d','q','A','B','C','D','Q']
 FileRead = io.open('city.txt', 'r', encoding='utf8')
 FileWrite = open('city_backup.txt', 'w+')
 menu = eval(FileRead.read())

 def Judge(Province,municipality):                                     #对菜单层级进行判断,返回该级菜单字典
     if (Province == 0)and(municipality == 0): return menu
     elif (Province != 0)and(municipality == 0): return menu[Province]
     else : return menu[Province][municipality]

 def OperationJudge(Province,city,choice):                            #对输入操作,决定下一步动作
     if choice == 'b' and Province !=0 and city !=0 : shi(Province)
     elif choice == 'b' and Province !=0 :shen()
     elif choice == 'q': exit()
     elif choice == 'd':FileDel(Province,city)
     elif choice == 'c':FileChance(Province,city)
     elif choice == 'a':FileAppend(Province,city)

 def FileAppend(Province,municipality):                               #增加项目模块
     MenuAppend = Judge(Province,municipality)
     CityAppend = raw_input("请输入添加的城市\n",)
     MenuAppend[CityAppend]={}
     ExChange(Province,municipality)

 def FileDel(Province,municipality):                                  #删除项目模块
     MenuDel = Judge(Province,municipality)
     CityDel = raw_input("请输入你要删除的城市\n",)
     MenuDel.pop(CityDel)
     ExChange(Province,municipality)

 def FileChance(Province,municipality):                              #改变项目模块
     MenuChance = Judge(Province,municipality)
     CityChance = raw_input("请输入被替换的城市\n",)
     if CityChance in MenuChance : CityChanced = raw_input("请输入替换的城市\n",)
     MenuChance[CityChanced] = MenuChance[CityChance]
     MenuChance.pop(CityChance)
     ExChange(Province,municipality)

 def ExChange(Province,municipality):                                #对改变的项目写入文件当中进行保存
     global FileRead,FileWrite
     menucopy =str(menu)
     FileWrite.write(menucopy)
     FileRead.close()
     FileWrite.close()
     os.remove('city.txt')
     os.renames('city_backup.txt','city.txt')
     FileRead = io.open('city.txt', 'r', encoding='utf8')
     FileWrite = open('city_backup.txt', 'w+')
     if (Province == 0)and(municipality == 0): shen()
     elif (Province != 0)and(municipality == 0): shi(Province)
     else : xian(Province,municipality)

 def shen():                                                         #第一级菜单查找
     for i in menu:print (i)
     Province = raw_input('请输入你选择的省份\n',)
     if Province in Operation : OperationJudge(0,0,Province)
     elif (Province in menu) and menu[Province]!= {}: shi(Province)
     elif Province not in menu :
         print '请输入正确的省份\n'
         shen()
     elif menu[Province] == {} :
         print "该省下面没有市,请添加\n"
         FileAppend(Province,0)
     elif Province == '':
         print '请输入正确的省份\n'
         shen()

 def shi(Province):                                                     #第二级菜单查找
     for i in menu[Province]: print (i)
     city = raw_input('请输入你选择的市或退出程序(Q)或返回上一级菜单(B)\n')
     if city in Operation : OperationJudge(Province,0,city)
     elif (city in menu[Province])and (menu[Province][city]!={}):xian(Province,city)
     elif  city not in menu[Province] :
         print  '请输入正确的市\n'
         shi(Province)
     elif menu[Province][city] == {} :
         print "该市下面没有县或区,请添加\n"
         FileAppend(Province,city)
     elif city == '':
         '请输入正确的市\n'
         shi(Province)

 def xian(Province,city):                                           #第三级菜单查找
     for i in menu[Province][city]: print (i)
     city2 = raw_input('请选择退出(Q)或返回(B)\n')
     if city2 in Operation : OperationJudge(Province,city,city2)
     else:
         print '请输入正确的区或县\n'
         xian(Province,city)

 shen()

2018-5-13

思路

三个操作模块,分别是 增/删/改 功能
三个查询模块,分别对每一级别进行查询

报错信息:
Traceback (most recent call last):
  File "E:/python/day8/three menu.py", line 79, in <module>
    shen()
  File "E:/python/day8/three menu.py", line 37, in shen
    elif city == 'a':FileAppend(menu)
  File "E:/python/day8/three menu.py", line 17, in FileAppend
    ExChange()
  File "E:/python/day8/three menu.py", line 47, in ExChange
    FileWrite.write(menu)
TypeError: expected a string or other character buffer object

 # -*- coding: UTF-8 -*-
 #__filename:three menu
 #__time:2018/5/13 22:04

 import io
 import os

 FileRead = io.open('city','r',encoding='utf8')
 FileWrite= open('city_backup','w+')
 menu = eval(FileRead.read())

 def FileAppend(MenuAppend):

     CityAppend = raw_input("请输入添加的城市",)
     MenuAppend['CityAppend']={}
     ExChange()

 def FileDel(MenuDel):
     CityDel = raw_input("请输入你要删除的城市",)
     MenuDel.pop('CityDel')
     ExChange()

 def FileChance(MenuChance):
     CityChance = raw_input("请输入被替换的城市",)
     if CityChance in MenuChance : CityChanced = raw_input("请输入替换的城市",)
     MenuChance['CityChanced'] = MenuChance['CityChance']
     MenuChance.pop('CityChance')
     ExChange()

 def shen():
     for i in menu:print (i)
     city = raw_input('请输入你选择的省份',)
     if city == 'q': exit()
     elif city == 'd':FileDel(menu)
     elif city == 'c':FileChance(menu)
     elif city == 'a':FileAppend(menu)
     elif (city in menu) and (menu[city]!=0): shi(city)
     elif menu[city]==0 :
         print "该省下面没有市,请添加"
         FileAppend(menu[city])
     else:
         print '请输入正确的省份'
         shen()

 def ExChange():
     FileWrite.write(menu)
     FileRead.close()
     FileWrite.close()
     os.remove('FileRead')
     os.renames('FileWrite','FileRead')

 def shi(city):
     for i in menu[city]: print (i)
     city1 = raw_input('请输入你选择的市或退出程序(Q)或返回上一级菜单(B)').strip()
     if city1 == 'b':shen()
     elif city1 == 'q':exit()
     elif city1 == 'd':FileDel(menu[city])
     elif city1 == 'c':FileChance(menu[city])
     elif city1 == 'a':FileAppend(menu[city])
     elif (city1 in menu[city])and (menu[city][city1]!=0):xian(city,city1)
     elif menu[city][city1]==0 : FileAppend( menu[city][city1])
     else:
         '请输入正确的市'
         shi(city)

 def xian(city,city1):
     for i in menu[city][city1]: print (i)
     city2 = raw_input('请选择退出(Q)或返回(B) ')
     if city2 == 'b': shi(city)
     elif city2 == 'q': exit()
     elif city2 == 'd':FileDel(menu[city][city1])
     elif city2 == 'c':FileChance(menu[city][city1])
     elif city2 == 'a':FileAppend(menu[city][city1])
     else:
         '请输入正确的区或县'
         xian(city, city1)

 shen()
上一篇:mysql修改root用户密码


下一篇:Linux命令vi/vim 使用方法讲解