Python Programming Quick Start 8th Chapter Practical Project Reference Answer

Source: Internet
Author: User

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

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.