Python 三级菜单 增强版
2018-06-18 02:54:05来源:未知 阅读 ()
需要实现的功能是:
三级菜单
1.从文本内读出选项
2.查询每一级的选项,并能对选项进行增/删/改功能
3.每一级可以退出程序或者返回上一层
2018-5-14 更新内容
思路
实现过程中的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)
项目代码
1 #coding=utf-8 2 #__author:Administrator 3 #__time:2018/5/14 17:34 4 #__file_name:22222 5 import io 6 import os 7 Operation =['a','b','c','d','q','A','B','C','D','Q'] 8 FileRead = io.open('city.txt', 'r', encoding='utf8') 9 FileWrite = open('city_backup.txt', 'w+') 10 menu = eval(FileRead.read()) 11 12 def Judge(Province,municipality): #对菜单层级进行判断,返回该级菜单字典 13 if (Province == 0)and(municipality == 0): return menu 14 elif (Province != 0)and(municipality == 0): return menu[Province] 15 else : return menu[Province][municipality] 16 17 def OperationJudge(Province,city,choice): #对输入操作,决定下一步动作 18 if choice == 'b' and Province !=0 and city !=0 : shi(Province) 19 elif choice == 'b' and Province !=0 :shen() 20 elif choice == 'q': exit() 21 elif choice == 'd':FileDel(Province,city) 22 elif choice == 'c':FileChance(Province,city) 23 elif choice == 'a':FileAppend(Province,city) 24 25 def FileAppend(Province,municipality): #增加项目模块 26 MenuAppend = Judge(Province,municipality) 27 CityAppend = raw_input("请输入添加的城市\n",) 28 MenuAppend[CityAppend]={} 29 ExChange(Province,municipality) 30 31 def FileDel(Province,municipality): #删除项目模块 32 MenuDel = Judge(Province,municipality) 33 CityDel = raw_input("请输入你要删除的城市\n",) 34 MenuDel.pop(CityDel) 35 ExChange(Province,municipality) 36 37 def FileChance(Province,municipality): #改变项目模块 38 MenuChance = Judge(Province,municipality) 39 CityChance = raw_input("请输入被替换的城市\n",) 40 if CityChance in MenuChance : CityChanced = raw_input("请输入替换的城市\n",) 41 MenuChance[CityChanced] = MenuChance[CityChance] 42 MenuChance.pop(CityChance) 43 ExChange(Province,municipality) 44 45 def ExChange(Province,municipality): #对改变的项目写入文件当中进行保存 46 global FileRead,FileWrite 47 menucopy =str(menu) 48 FileWrite.write(menucopy) 49 FileRead.close() 50 FileWrite.close() 51 os.remove('city.txt') 52 os.renames('city_backup.txt','city.txt') 53 FileRead = io.open('city.txt', 'r', encoding='utf8') 54 FileWrite = open('city_backup.txt', 'w+') 55 if (Province == 0)and(municipality == 0): shen() 56 elif (Province != 0)and(municipality == 0): shi(Province) 57 else : xian(Province,municipality) 58 59 def shen(): #第一级菜单查找 60 for i in menu:print (i) 61 Province = raw_input('请输入你选择的省份\n',) 62 if Province in Operation : OperationJudge(0,0,Province) 63 elif (Province in menu) and menu[Province]!= {}: shi(Province) 64 elif Province not in menu : 65 print '请输入正确的省份\n' 66 shen() 67 elif menu[Province] == {} : 68 print "该省下面没有市,请添加\n" 69 FileAppend(Province,0) 70 elif Province == '': 71 print '请输入正确的省份\n' 72 shen() 73 74 def shi(Province): #第二级菜单查找 75 for i in menu[Province]: print (i) 76 city = raw_input('请输入你选择的市或退出程序(Q)或返回上一级菜单(B)\n') 77 if city in Operation : OperationJudge(Province,0,city) 78 elif (city in menu[Province])and (menu[Province][city]!={}):xian(Province,city) 79 elif city not in menu[Province] : 80 print '请输入正确的市\n' 81 shi(Province) 82 elif menu[Province][city] == {} : 83 print "该市下面没有县或区,请添加\n" 84 FileAppend(Province,city) 85 elif city == '': 86 '请输入正确的市\n' 87 shi(Province) 88 89 def xian(Province,city): #第三级菜单查找 90 for i in menu[Province][city]: print (i) 91 city2 = raw_input('请选择退出(Q)或返回(B)\n') 92 if city2 in Operation : OperationJudge(Province,city,city2) 93 else: 94 print '请输入正确的区或县\n' 95 xian(Province,city) 96 97 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
1 # -*- coding: UTF-8 -*- 2 #__filename:three menu 3 #__time:2018/5/13 22:04 4 5 import io 6 import os 7 8 FileRead = io.open('city','r',encoding='utf8') 9 FileWrite= open('city_backup','w+') 10 menu = eval(FileRead.read()) 11 12 def FileAppend(MenuAppend): 13 14 CityAppend = raw_input("请输入添加的城市",) 15 MenuAppend['CityAppend']={} 16 ExChange() 17 18 def FileDel(MenuDel): 19 CityDel = raw_input("请输入你要删除的城市",) 20 MenuDel.pop('CityDel') 21 ExChange() 22 23 def FileChance(MenuChance): 24 CityChance = raw_input("请输入被替换的城市",) 25 if CityChance in MenuChance : CityChanced = raw_input("请输入替换的城市",) 26 MenuChance['CityChanced'] = MenuChance['CityChance'] 27 MenuChance.pop('CityChance') 28 ExChange() 29 30 def shen(): 31 for i in menu:print (i) 32 city = raw_input('请输入你选择的省份',) 33 if city == 'q': exit() 34 elif city == 'd':FileDel(menu) 35 elif city == 'c':FileChance(menu) 36 elif city == 'a':FileAppend(menu) 37 elif (city in menu) and (menu[city]!=0): shi(city) 38 elif menu[city]==0 : 39 print "该省下面没有市,请添加" 40 FileAppend(menu[city]) 41 else: 42 print '请输入正确的省份' 43 shen() 44 45 def ExChange(): 46 FileWrite.write(menu) 47 FileRead.close() 48 FileWrite.close() 49 os.remove('FileRead') 50 os.renames('FileWrite','FileRead') 51 52 def shi(city): 53 for i in menu[city]: print (i) 54 city1 = raw_input('请输入你选择的市或退出程序(Q)或返回上一级菜单(B)').strip() 55 if city1 == 'b':shen() 56 elif city1 == 'q':exit() 57 elif city1 == 'd':FileDel(menu[city]) 58 elif city1 == 'c':FileChance(menu[city]) 59 elif city1 == 'a':FileAppend(menu[city]) 60 elif (city1 in menu[city])and (menu[city][city1]!=0):xian(city,city1) 61 elif menu[city][city1]==0 : FileAppend( menu[city][city1]) 62 else: 63 '请输入正确的市' 64 shi(city) 65 66 def xian(city,city1): 67 for i in menu[city][city1]: print (i) 68 city2 = raw_input('请选择退出(Q)或返回(B) ') 69 if city2 == 'b': shi(city) 70 elif city2 == 'q': exit() 71 elif city2 == 'd':FileDel(menu[city][city1]) 72 elif city2 == 'c':FileChance(menu[city][city1]) 73 elif city2 == 'a':FileAppend(menu[city][city1]) 74 else: 75 '请输入正确的区或县' 76 xian(city, city1) 77 78 shen()
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:PyCharm中设置字体大小
下一篇:python网络编程(五)
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash