The 8th chapter of the Practical project of the crazy lyrics
Create a crazy lyrics (Mad Libs) program that will read into a text file and let the user appear in the text file where Adjective,noun,verb and other words are placed, plus their own text.
First, prepare a a.txt text file
The program code is as follows:
#!/usr/bin/env python3.4#Coding:utf-8#8.9.2Importref1= Open ('a.txt','R') Strf1=F1.read ()Print("the contents of the original file are:")Print(strf1) strf1_list= Strf1.split (' ') F1.close ()#because the original file needs to be replaced with words that are uppercase English words#use regular expressions to find all the words in the original file that will be replacedReplist = Re.findall (r'[A-z]{2,}', Strf1)Print("the word that will be replaced in the original file is:")Print(replist)Print() forRepinchReplist:inputstr= Input ("Enter%s"%Rep)Print(INPUTSTR)#Insert the replaced word into the original list position firstStrf1_list.insert (Strf1_list.index (Rep), Inputstr)#Delete the original word againStrf1_list.remove (Rep)#convert a list to a stringNewstr =' '. Join (strf1_list)Print("the replaced content is:")#print (NEWSTR)#writes a new string to the file B.txt and prints it to the screenF2 = open ('B.txt','w+') F2.write (NEWSTR) f2.close () F3= Open ('B.txt','R')Print(F3.read ()) F3.close ()
The output is:
Cat down B.txt
OK is done.
Python Programming Quick Start 8th Chapter Practical Project Reference Answer