我正在尝试创建MadLibs游戏进行练习。我将每个故事保存在一个.txt文件中,第一个空白由_
表示,第二个空白由__
表示,第三个空白___
依此类推。我正在尝试创建一个循环,该循环通过递增_
和通过数组的计数来用用户输入的单词替换每个空格。我不确定这是否是最简单/正确的方法,但这就是我能想到的。这是我到目前为止的代码:
storyone = open("storyone.txt", "rt")
contentone = storyone.read()
words = ["blank", "blank", "blank", "blank", "blank", "blank", "blank", "blank", "blank", "blank", "blank", "blank"]
words[0] = input("Enter an Adjective: ")
words[1] = input("Enter a Noun: ")
words[2] = input("Enter a Verb (past-tense): ")
words[3] = input("Enter an Adverb: ")
words[4] = input("Enter an Adjective: ")
words[5] = input("Enter a Noun: ")
words[6] = input("Enter a Noun: ")
words[7] = input("Enter an Adjective: ")
words[8] = input("Enter a Verb: ")
words[9] = input("Enter a Adverb: ")
words[10] = input("Enter a Verb (past-tense): ")
words[11] = input("Enter an Adjective: ")
line = "_"
count = 0
for line in contentone:
contentone.replace(line, words[count])
count += 1
line += "_"
break
storyone.close()
print(contentone)
我希望代码通过并用输入的相应单词替换_
字符串中的每个contentone
。