疯狂填词程序:
它将读入文本文件,并让用户在该文本文件中出现 ADJECTIVE、NOUN、ADVERB 或 VERB 等单词的地方,
加上他们自己的文本。
例如,一个文本文件可能看起来像这样:
The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN wasunaffected by these events.
程序将找到这些出现的单词,并提示用户取代它们
Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck
以下的文本文件将被创建:
The silly panda walked to the chandelier and then screamed. A nearby pickuptruck was unaffected by these events.
mystr='The ADJECTIVE panda walked to the NOUN and then VERB . A nearby NOUN was unaffected by these events'
print('原字符串:'+mystr)
mylist=mystr.split(' ')
print(mylist)
for i in range(len(mylist)):
str=mylist[i]
if str=='ADJECTIVE' or str=='NOUN' or str=='VERB'or str=='ADVERB':
user_input=input('Enter an %s'%(str.lower()))
mylist[i]=user_input
newstr=' '.join(mylist)
print('新字符串:'+newstr)