Mad Libs游戏Python中同一单词的多次出现
我正在尝试制作一个Mad Libs游戏,用户输入在句子中多次出现的单词。目前,我发现用户需要多次输入相同的单词Mad Libs游戏Python中同一单词的多次出现,python,Python,我正在尝试制作一个Mad Libs游戏,用户输入在句子中多次出现的单词。目前,我发现用户需要多次输入相同的单词 sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth. We only know because NUMBER years ago, MUSICIAN went on an epic quest with only a OBJECT for
sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth.
We only know because NUMBER years ago,
MUSICIAN went on an epic quest with only a OBJECT for
company. MUSICIAN had to steal GENRE_OF_MUSIC from PERSON
and did this by playing a game of SPORT as a distraction."""
#Words to be replaced
parts_of_speech = ["MUSICIAN", "GENRE_OF_MUSIC", "NUMBER",
"OBJECT", "PAST_TENSE_VERB", "PERSON", "SPORT"]
# Checks if a word in parts_of_speech is a substring of the word passed in.
def word_in_pos(word, parts_of_speech):
for pos in parts_of_speech:
if pos in word:
return pos
return None
def play_game(ml_string, parts_of_speech):
replaced = []
ml_string = ml_string.split()
for word in ml_string:
replacement = word_in_pos(word, parts_of_speech)
if replacement != None:
user_input = raw_input("Type in a: " + replacement + " ")
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
print play_game(sentence_a, parts_of_speech)
当您运行代码时,我希望用户只输入一次音乐的流派,并且Mad Libs语句只在每次出现时使用该条目 您没有跟踪替换以处理重复的关键字 您可以更改代码以循环浏览关键字:
sentence_a = """GENRE_OF_MUSIC was created by PERSON in Middle Earth.
We only know because NUMBER years ago,
MUSICIAN went on an epic quest with only a OBJECT for
company. MUSICIAN had to steal GENRE_OF_MUSIC from PERSON
and did this by playing a game of SPORT as a distraction."""
#Words to be replaced
parts_of_speech = ["MUSICIAN", "GENRE_OF_MUSIC", "NUMBER",
"OBJECT", "PAST_TENSE_VERB", "PERSON", "SPORT"]
def play_game(ml_string, parts_of_speech):
for word in parts_of_speech:
if word in ml_string:
user_input = raw_input("Type in a: " + word + " ")
ml_string = ml_string.replace(word, user_input)
return ml_string
print play_game(sentence_a, parts_of_speech)
我建议使用字符串格式,而不是搜索和替换 以下是它的工作原理:
import string
from textwrap import dedent
FMT = string.Formatter()
def get_field_names(format_string):
"""
Return a list of unique field names from the format string
"""
return sorted(set(p[1] for p in FMT.parse(format_string) if p[1] is not None))
sentence_a = dedent("""
{GENRE_OF_MUSIC} was created by {PERSON} in Middle Earth.
We only know because {NUMBER} years ago, {MUSICIAN} went
on an epic quest with only a {OBJECT} for company.
{MUSICIAN} had to steal {GENRE_OF_MUSIC} from {PERSON}
and did this by playing a game of {SPORT} as a distraction.
""")
parts_of_speech = get_field_names(sentence_a)
replace_dict = {pos:pos for pos in parts_of_speech}
# after getting input from player
replace_dict["MUSICIAN"] = "Chuck Berry"
# show result
print(sentence_a.format(**replace_dict))
你的代码太复杂了 首先,您可以要求用户替换词性。您可以通过对语音部分的循环来实现这一点,并将每个用户输入存储到映射中:
parts = {}
for replacement in parts_of_speech:
user_input = raw_input("Type in a " + replacement + ": ")
parts[replacement] = user_input
然后,您可以拆分句子并用用户替换词(如果存在)替换每个单词。如果没有,你就遵守诺言:
words = [parts.get(word, word)
for word in ml_string.split()]
return " ".join(words)
你会得到:
Type in a MUSICIAN: Bach
Type in a GENRE_OF_MUSIC: piano
Type in a NUMBER: 23
Type in a OBJECT: roller
Type in a PAST_TENSE_VERB: dig
Type in a PERSON: Mr president
Type in a SPORT: football
piano was created by Mr president in Middle Earth. We only know because 23 years ago, Bach went on an epic quest with only a roller for company. Bach had to steal piano from Mr president and did this by playing a game of football as a distraction.
注意:word_in_pos功能是无用的。将您已经在dict中完成的功能添加到dict中,然后检查它们是否在dict中,看看这是否是您想要的功能:这一功能非常有用,感谢Eric,感谢其他人,我将与其他人一起试验,并确保在未来的项目中使用它们!谢谢,我试着让我的答案尽可能接近你的原始代码,这样更容易理解。我建议你理解其他答案,我喜欢。