用pythonmadlibs程序自动化那些无聊的东西

用pythonmadlibs程序自动化那些无聊的东西,python,Python,我是一个初学者程序员,所以p;请耐心听我说,我正在写这本书,并坚持做练习。指导如下 疯狂填词 创建一个Mad Libs程序,读取文本文件并允许用户添加 他们自己的文本中任何地方都有形容词、名词、副词或动词 显示在文本文件中。例如,文本文件可能如下所示: 形容词panda依次指向名词和动词。附近的一个名词是 不受这些事件的影响。 程序将查找这些事件并提示用户 替换它们。 输入一个形容词: 愚蠢的 输入一个名词: 吊灯 输入动词: 尖叫 输入一个名词: 皮卡车 然后将创建以下文本文件: 愚蠢的熊猫走

我是一个初学者程序员,所以p;请耐心听我说,我正在写这本书,并坚持做练习。指导如下 疯狂填词 创建一个Mad Libs程序,读取文本文件并允许用户添加 他们自己的文本中任何地方都有形容词、名词、副词或动词 显示在文本文件中。例如,文本文件可能如下所示: 形容词panda依次指向名词和动词。附近的一个名词是 不受这些事件的影响。 程序将查找这些事件并提示用户 替换它们。 输入一个形容词: 愚蠢的 输入一个名词: 吊灯 输入动词: 尖叫 输入一个名词: 皮卡车 然后将创建以下文本文件: 愚蠢的熊猫走向吊灯,然后尖叫起来。附近的小货车 卡车没有受到这些事件的影响。 结果应打印到屏幕上并保存到新的文本文件中

import pyperclip

def ML(file):
Ofile=open(file)
x=Ofile.read()
y=x.split()
for i in range(len(y)):
    if y[i]=='ADJECTIVE':
        print('what is your adjective?')
        replacment=input()
        y[i]=replacment
        for i in range(len(y)):
            if y[i]=='NOUN':
                print('what is your noun?')
                replacment=input()
                y[i]=replacment
                for i in range(len(y)):
                    if y[i]=='VERB':
                        print('what is your verb?')
                        replacment=input()
                        y[i]=replacment
for i in range(len(y)):
    print(y[i],end=' ')
Nfile=open('madlibs3.txt.txt','w')
Nfile.write(x)
Nfile.close()
Ofile.close()
print('write your file path.')
Afile = input()
ML(Afile)
我的问题是我的代码无法工作


我很确定主要的问题是新字符串没有保存在变量x中,但是我不知道怎么做

您可以将代码简化如下

代码

def ML(input_file, output_file):
  # Use with since it automatrically closes files
  with open(input_file, 'r') as ifile, open(output_file, 'w') as ofile:
    result = []
    for line in ifile:  # looping through lines in file
      new_line = []
      for word in line.rstrip().split():  # looping through words in a line
        if word in ('NOUN', 'ADJECTIVE', 'VERB'):  # word is one of the ones we're checing for
          replace_word = input(f'What is your {word.lower()}?')
          new_line.append(replace_word)  # use replacement word
        else:
          new_line.append(word)          # use original word
      result.append(' '.join(new_line))  # form new space separated line

    # Write result
    ofile.write('\n'.join(result))       # join all lines together with carriage returns
                                         # and write to file

    return result                        # Return result to caller
print(ML('input.txt', 'output.txt'))     # print results to screen and
                                         # writes to 'output.txt'
用法

def ML(input_file, output_file):
  # Use with since it automatrically closes files
  with open(input_file, 'r') as ifile, open(output_file, 'w') as ofile:
    result = []
    for line in ifile:  # looping through lines in file
      new_line = []
      for word in line.rstrip().split():  # looping through words in a line
        if word in ('NOUN', 'ADJECTIVE', 'VERB'):  # word is one of the ones we're checing for
          replace_word = input(f'What is your {word.lower()}?')
          new_line.append(replace_word)  # use replacement word
        else:
          new_line.append(word)          # use original word
      result.append(' '.join(new_line))  # form new space separated line

    # Write result
    ofile.write('\n'.join(result))       # join all lines together with carriage returns
                                         # and write to file

    return result                        # Return result to caller
print(ML('input.txt', 'output.txt'))     # print results to screen and
                                         # writes to 'output.txt'

请重复介绍之旅,尤其是和。“我的代码不起作用”不是一个问题规范。“我不明白怎么做”不是堆栈溢出问题,因为这不是编码或教程服务。阅读发帖指南,如果你对本网站的章程有任何疑问,请将你的帖子编辑成适用的问题。多谢你的代码教会了我很多,并修复了我的旧问题code@HamzaAshfaq--很高兴这有帮助。别忘了。