Python Foundation 12th Day

Source: Internet
Author: User

A: Three-level menu exit method (the use of tag)

tag=True whiletag:Print('Level1') Choice=input ('Level1>>:'). Strip ()ifChoice = ='quit': Break    ifChoice = ='Quit_all': Tag =False whiletag:Print('Level2') Choice=input ('Level2>>:'). Strip ()ifChoice = ='quit': Break        ifChoice = ='Quit_all': Tag =False whiletag:Print('Level3') Choice= Input ('Level3>>:'). Strip ()ifChoice = ='quit': Break            ifChoice = ='Quit_all': Tag=false

II: Processing of documents (increase and deletion of search)

ImportOSdefFile_handler (backend_data,res='None', type='Fetch'):    ifType = ='Fetch': With open ('Renkoupucha','R') as Read_f:tag=false#identify a Stateret=[]             forRead_lineinchRead_f:ifRead_line.strip () = = Backend_data:#Remove Career Spacetag=TrueContinue                ifTag andRead_line.startswith ('Backend'):                     Break                iftag:Print(read_line,end="') ret.append (read_line)returnretelifType = =' Change': With open ('Renkoupucha','R') as Read_f, open ('renkoupucha_new','W') as Write_f:tag=False Has_write=False forRead_lineinchRead_f:ifRead_line.strip () = =Backend_data:tag=TrueContinue                ifTag andRead_line.startswith ('Backend'): Tag=Falseif  nottag:write_f.write (read_line)Else:                    if  notHas_write: forRecordinchRes:write_f.write (record) Has_write=True Os.rename ('Renkoupucha','Renkoupucha.bak') Os.rename ('renkoupucha_new','Renkoupucha') Os.remove ('Renloupucha.bak')defFetch (data): Backend_data='Backend%s'%DatareturnFile_handler (backend_data)defAdd ():PassdefChange (data):#print (' This is the modification function ')    Print('the data entered by the user is', data)#Data[0] #文档当中的一条记录backend=data[0]['Backend'] Backend_data='Backend%s'%Backend old_server_recored='%s Server%s%s weight%s Maxconn%s'%(' '*8,data[0]['Record']['Server'], data[0]['Record']['Server'], data[0]['Record']['Weight'], data[0]['Record']['Maxconn'],) new_server_recored='%s Server%s%s weight%s Maxconn%s'% (' '* 8, data[1]['Record']['Server'], data[1]['Record']['Server'], data[1]['Record']['Weight'], data[1]['Record']['Maxconn'],)    Print('the record that the user wants to modify is', old_server_recored) Res= Fetch (data[0]['Backend'])    Print(RES)if  notResorOld_server_recored not inchRes:return 'Modify record does not exist'    Else: Index=Res.index (old_server_recored) Res[index]=new_server_recored#Modified ListRes.insert ('%s\n'%backend_data) res.insert (0,'%s\n'%backend_data) File_handler (backend_data,res=res,type=' Change')defDelete ():Passif __name__=='__main__': Msg=" "1: Query 2: Add 3: Modify 4: Delete 5: Exit" "Msg_dic={        '1': Fetch,'2': Add,'3': Change,'4':d elete,} whileTrue:Print(msg) Choice=input ('Please enter your options:'). Strip ()if  notChoiceContinue        ifChoice = ='5': BreakData=input ('Please enter your data:'). Strip ()ifChoice! ='1': Data=eval (data) Res=Msg_dic[choice] (data)Print('Final Result', res)

Python Foundation 12th Day

Related Article

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.