Python little Experiment, crazy lyrics

Source: Internet
Author: User

#! python# coding: utf-8# #创建一个疯狂填词 (mad libs) program, which reads into a text file and lets the user appear in the text file adjective, NOUN, adverb   or verb  and other words, plus their own text. For example, a text file might look like this:# the adjective panda walked to the noun and  Then verb. a nearby noun wasunaffected by these events. #程序将找到这些出现的单词, and prompt the user to replace them. #Enter  an adjective: #silly #enter a noun: #chandelier #enter a verb: #screamed #enter  a noun: Text files below #pickup  truck# will be created: #The  silly panda walked to the  chandelier and then screamed. a nearby pickup truck was  Unaffected by these events. #结果应该打印到屏幕上 and Save as a new text file. The #  program code is as follows: Import ref1 = open (' a.txt ', ' R ') print ("Original file content:") print ( STRF1) #将字符串以空格为分隔符生成一个列表strf1_list  = strf1.split () #去除列表中元素结尾的逗号和句号, and gets the index and value of the element with commas and periods saved in a dictionary i =  0j = {}for strs in strf1_list:    if  '. '  in strs or  ', '  in strs:        j[i]= strs        strf1_list[i] = strs[:-1]     i += 1print ("Print out a list without periods and commas") print (strf1_list) print ("Print out element index and value with a period with a comma") print (j) f1.close () #   because the original file needs to be replaced by words that are capitalized English words #   use regular expressions to find all the words in the original file that will be replaced Replist = re.findall (R ' [A-z]{2,} ', STRF1) print (the word to be replaced in the original file is: ") print (replist) for rep in replist:    # The letter at the beginning of the vowel letter does not indicate the same message     if rep[0] in  ' Aeiou ':         inputstr = input ("enter an %s "  % rep)      else:        inputstr = input ("Enter a %s   " % rep"     print (INPUTSTR)     #  inserts the replaced words into the original list at the corresponding position     strf1_list.insert (Strf1_list.index (Rep), Inputstr)     #  Delete the original word     strf1_list.remove (Rep) # Adds a comma or period back to the element above which the comma or period is removed, and according to the J dictionary you can find exactly the element For key,value in j.items () with a comma or period at the end ():    Strf1_list[key] = str (Strf1_list[key])  + str (value[-1]) #  convert list to string newstr =  '   '. Join (strf1_list) print ("Replace with:")   #print (NEWSTR) #  writes a new string to the file B.txt and prints to the screen f2 =  Open (' B.txt ', ' w+ ') f2.write (NEWSTR) f2.close ()


This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1931576

Python little Experiment, crazy lyrics

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.