Python:疯狂的自由人

时间:2015-09-21 02:59:52

标签: python

您好我有Mad libs的这个项目,但我不知道如何制作一个功能,询问用户他们想要的简单,中等或难度之间的难度,并根据他们的答案将他们重定向到所需的水平疯狂的自由。这就是我到目前为止所拥有的。感谢。

parts_of_speech_words = ["VERB","PLACE","ADJECTIVE","NOUN","PLURALNOUN","ADVERB"]

level_easy = """I __VERB__ to go to the __PLACE__
             but I don't go in the __ADJECTIVE__
             I am __ADJECTIVE__ of the __NOUN__
             and getting __VERB__ by a __NOUN__."""


level_medium = """Begging to hear __NOUN__
             My ears remain __ADJECTIVE__ 
             __NOUN__ for signs
             my __NOUN__ remain __VERB__
             Yet __NOUN__still VERB."""


level_hard = """I __VERB__ you without __NOUN__
             how, or when, or from where,
             I love you __ADVERB__,
             without __PLURALNOUN__ or pride;
             So I love you because I know
             no other way that this:
             Where I does not VERB, nor you,
             so close that your NOUN
             on my chest is my hand,
             so close that your NOUN close
             as I fall ADJECTIVE."""

greeting = raw_input ("Welcome to mad libs, What's your name? ")
prompt = raw_input ("Select your level: easy, medium or hard: ")

def entry_level_prompt (variable_level):
    easy = level_easy
    medium = level_medium
    hard = level_hard
    for e in variable_level:
        if prompt == easy:
            return level_easy
            print level_easy
        if prompt == medium:
            return level_medium
            print level_medium
        if prompt == hard:
            return level_hard
            print lever_hard
print "Ok %s you chose the %s level" % (greeting , prompt)
print entry_level_prompt (variable_level)

def parts_of_speech (words_string, list_of_part_of_speech):
    for pos in list_of_part_of_speech:
        if pos in words_string:
            return pos
    return None

def play_mad_libs (split_string, list_of_part_of_speech):
    replaced = []
    split_string = split_string.split ()
    for words_string in split_string:
        replacement = parts_of_speech (words_string, list_of_part_of_speech)
        if replacement != None:
            user_input = raw_input ("Type in a: " + replacement + " ")
            words_string = words_string.replace (replacement, user_input)
            replaced.append(words_string)
        else:
            replaced.append(words_string)
    replaced = " ".join(replaced)
    return replaced

print play_mad_libs (entry_level_prompt, parts_of_speech_words)

2 个答案:

答案 0 :(得分:1)

你把选择器 - “简单”,“中等”或“硬”与你想要返回的变量混淆了 - level_easy,level_medium或level_hard。您不能同时将变量提示用于这两个目的。

我建议你在开始时保持变量提示:它保存用户输入。然后,只需测试它并返回所需的脚本:

if prompt == "easy":
    return level_easy
elif prompt == "medium"
    return level_medium
elif prompt == "hard"
    return level_hard
else:
    "Please enter easy, medium, or hard for the level."

答案 1 :(得分:1)

您的代码中存在错误。您正在调用 String userString = name.substring(0,1).toUpperCase() + name.substring(1, name.length()).toLowerCase(); ,但entry_level_prompt(variable_level)在方法范围之外不存在。

要控制难度,您可以创建一个名为variable_level

的方法
get_difficulty()