在循环中追加意外输出(Python)

时间:2017-07-25 22:35:54

标签: python list loops for-loop append

对不起,继承人的代码

level_1 = "The _1_ man in the _2_ place can make all the _3_ in the _4_"

level_2 = "We both _1_ a lot of _2_ that you're going to _3_. But I _4_ \ we 
can put our _5_ behind us. For science. " \
      "You _6_ "

level_3 = "Everyone I have _1_ for has either _2_ or left me._3_ f***ing 
except for \ _4_! So don't _5_ me I'd be _6_ " \
      "with somebody else, because the _7_ is I would just be more _8_ "

blank_words = ["_1_", "_2_", "_3_", "_4_", "_5_", "_6_", "_7_", "_8_", ]

level_1_answers = ["right", "wrong", "difference", "world"]

level_2_answers = ["said", "things", "regret", "think", "differences", 
"monster"]

level_3_answers = ["cared", "died", "everyone", "you", "tell", "safer", 
"truth", "scared"]




def level_selector():
    """Gets input from the user to see which level the game starts on"""

    while True:
        try:
             level = int(raw_input("There are 3 levels, pick which to start 
             with \n -->"))
        except ValueError:
            print "Please select a NUMBER"
        else:
            if 1 <= level <= 3:
                if level == 1:
                    return level_1, level_1_answers, level

                elif level == 2:
                    return level_2, level_2_answers, level

                 elif level == 3:
                    return level_3, level_3_answers, level

            else:
                print "Select a number within the level range"


def blanks_to_fill(word, blanks):
    for blank in blanks:
        if blank in word:
            return blank
    return None


def fill_blanks(word, replaced, blanks, user_input, index):
    if blanks_to_fill(word, blanks) is None:
        if word not in replaced:
            replaced.append(word)
    else:
        replacement = blanks_to_fill(word, blanks)
        word = word.replace(replacement, user_input.upper())

        if replacement == blanks[index]:
            if replacement not in replaced:
                replaced.append(word)
            else:
                position = replaced.index(replacement)
                replaced[position] = word
        else:
            replaced.append(replacement)
    return replaced


def format_answers(quote, blanks, replaced, user_input, index):
    split_quote = quote.split()

    if type(replaced) == str:
        replaced = replaced.split()

    for word in split_quote:
        fill_blanks(word, replaced, blanks, user_input, index)

    replaced = " ".join(replaced)
    return replaced


def user_answer(level, quote, answers):
    replaced = []
    user_input = ""
    index = 0
    blank_range = len(answers)

    for blank in blank_words[:blank_range]:

        prompt = "Fill the blank for " + blank
        print prompt
        user_input = raw_input("-->")
        user_input = user_input.lower()

        while user_input != answers[index]:
            print "Incorrect, please try again."

            user_input = raw_input("-->")
            user_input = user_input.lower()

        print "Good job!"

        replaced = format_answers(quote, blank_words, replaced, user_input, 
    index)
        print replaced

        index += 1

    return replaced, index


def play_again():
    user_response = raw_input("Would you like to play again, yes or 
no?").lower()
    if user_response.startswith("y"):
        play()
    else:
        pass


def play():
    quote, answers, level = level_selector()
    print quote

    replaced = user_answer(level, quote, answers)

    print "Well done, you finished the level"

    play_again()


play()

我想要获得的输出是来自所选级别的引用以及来自玩家的正确响应。相反,我得到了像http://i.imgur.com/ESAOFBB.png这样的输出,其中缺少单词,而#34;空白&#34;占位符在输出的末尾被标记。从我所看到的问题出现在&#34; fill_blanks&#34;或者&#34; format_answers&#34;功能,但我自己也看不到。

也许一副新鲜的眼睛可以散发一些光芒。

非常感谢,如果你做出回应,我已经尝试过大部分时间自己做,但我只是打了一堵砖墙

0 个答案:

没有答案