需要實現的功能是:三級菜單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()