Python 模块的参数可以用字典么?哪位大神看看我的代码哪里有问题,报错信息也在的
程序员文章站
2022-03-18 15:47:03
需要实现的功能是:三级菜单1.从文本内读出选项2.查询每一级的选项,并能对选项进行增/删/改功能3.每一级可以退出程序或者返回上一层思路三个操作模块,分别是 增/删/改 功能三个查询模块,分别对每一级别进行查询报错信息:Traceback (most recent call last): File ......
需要实现的功能是:
三级菜单
1.从文本内读出选项
2.查询每一级的选项,并能对选项进行增/删/改功能
3.每一级可以退出程序或者返回上一层
思路
三个操作模块,分别是 增/删/改 功能
三个查询模块,分别对每一级别进行查询
报错信息:
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()