Python 填空选择其他答案时出错,但给出的答案除外

Python 填空选择其他答案时出错,但给出的答案除外,python,Python,这是我正在做的填空测验。当给出难度选择时,会出现问题。在函数def中填入空格(): 当用户键入难度轻松、中等或硬以外的内容时,游戏将结束。我希望它能够循环运行,直到用户选择合适的答案 我想让它使用def开始游戏(玩家难度): 我不知道当用户在难度选择中写下其他东西时,如何防止它结束游戏 #easy-mode driver_knowledge_test = ["When you're going to drive it is important to always put on your

这是我正在做的填空测验。当给出难度选择时,会出现问题。在函数
def中填入空格():

当用户键入难度
轻松
中等
以外的内容时,游戏将结束。我希望它能够循环运行,直到用户选择合适的答案

我想让它使用
def开始游戏(玩家难度):

我不知道当用户在难度选择中写下其他东西时,如何防止它结束游戏

#easy-mode
driver_knowledge_test = ["When you're going to drive it is important to always put on your ___1___, including your passengers.",
                        "If there are no lanes marked on the road, you should drive in the ___2___ side of the road.",
                        "It's getting dark and the sun is fading, you should turn on the ___3___.",
                        "Before driving on a freeway, you should make sure you have enough ___4___, oil, water and the correct tyre pressure."]
answer_1 = ['seatbelts', 'left', 'light', 'fuel']
answered_list_1 = ["When you're going to drive it is important to always put on your seatbelts, including your passengers.",
                    "If there are no lanes marked on the road, you should drive in the left side of the road.",
                    "It's getting dark and the sun is fading, you should turn on the light. ",
                    "Before driving on a freeway, you should make sure you have enough fuel, oil, water and the correct tyre pressure."]

#medium-mode
general_food_test = ["All fruits have ___1___ in them. That's what makes them different from vegetables.",
                    "You shouldn't drink coffee or ___2___ when you're going to sleep. It keeps you up at night.",
                    "It is not safe to eat ___3___ undercooked. It might be contaminated with a bacteria called Salmonella.",
                    "The essentials to make a cake are flour, ___4___, vanilla extract, unsalted butter and egg."]
answer_2 = ['seeds', 'tea', 'chicken', 'sugar']
answered_list_2 = ["All fruits have seeds in them. That's what makes them different from vegetables.",
                    "You shouldn't drink coffee or tea when you're going to sleep. It keeps you up at night.",
                    "It is not safe to eat chicken undercooked. It might be contaminated with a bacteria called Salmonella.",
                    "The essentials to make a cake are flour, sugar, vanilla extract, unsalted butter and egg."]

#hard-mode
general_health_test = ["An ___1___ a day, keeps the doctor away.",
                        "Too much dietary intake of can be ___2___ for you. However, it is unlikely.",
                        "You can check your pulse on the wrist, ___3___, and thigh.",
                        "The averag amount of ___4___ adults need is 7-9 hours."]
anwser_3 = ['apple', 'harmful', 'neck', 'sleep']
answered_list_3 = ["An apple a day, keeps the doctor away.",
                        "Too much dietary intake of can be harmful for you. However, it is unlikely.",
                        "You can check your pulse on the wrist, neck, and thigh.",
                        "The averag amount of sleep adults need is 7-9 hours."]

blank_space = ["___1___", "___2___", "___3___", "___4___"]



#Inputs raw_input's name
#Outputs introduction with user's name
def Intro(name_of_player):
    print "Hello " + name_of_player + ", we are going to play fill-in-the-blanks! Topics revolve around our everyday live, so don't worry if you didn't study much."
    print "Please note the game is case-sensitive. i.e. all capslock must be disabled."
    print "Let's get started, pick a difficulty."

#returns user's choice of list
def beginning_of_game(player_difficulty):
    while True:
        if player_difficulty == "easy":
            print "You chose 'easy'\n"
            return driver_knowledge_test, answer_1, answered_list_1 #use return function
        elif player_difficulty == "medium":
            print "You chose 'medium'\n"
            return general_food_test, answer_2, answered_list_2
        elif player_difficulty == "hard":
            print "You chose 'hard'\n"
            return general_health_test, answer_3, answered_list_3
        else:
            print "The following difficulty does not exist. Try again!"


#checks user input correct answer, and print out their progress. If user input incorrect, prompts to re-try
def answer_check(answer, sentence, blank_space, answered_list):
    new_list = []
    index = 0
    sentence_index = 0
    answer_index = 0
    blank_index = 0
    answered_list_index = 0
    total_of_sentence = 4 #the number of elements in the sentence list.
    print sentence[sentence_index]
    while index < total_of_sentence:
        print sentence[sentence_index]
        player_answer = raw_input("\nType your answer for " + blank_space[blank_index] + " \n>>>")
        if player_answer.lower() == answer[answer_index]:
            #new_list.append(sentence[sentence_index])
            new_list.append(answered_list[answered_list_index])
            #current fill-in-the-blanks that's been already answered
            print sentence[sentence_index].replace(blank_space[blank_index], answer[answer_index])
            answered_list_index += 1
            answer_index += 1
            sentence_index += 1
            index += 1
            blank_index += 1
            print "\nCCORRECT! \n"
        else:
            print "Wrong! Try again. :) \n"
    print sentence
    return "\nCongratulation! You completed the game!"




#inputs None
#Outputs whole program-functions
def fill_in_the_blanks():
    name_of_player = raw_input("enter your name \n>>>")
    Intro(name_of_player)

    difficulty_selection = raw_input("\nselect from easy, medium, or hard \n>>>")

    if difficulty_selection == "easy" or difficulty_selection == "medium" or difficulty_selection == "hard":
        sentence, answers, answered_sentence = beginning_of_game(difficulty_selection)
        print sentence

        answer_check(answers, sentence, blank_space, answered_sentence)



    print "\nThank you for playing!"









#initiate game from here.
fill_in_the_blanks()
#简易模式
驾驶员知识测试=[“当你要开车时,一定要穿上你的uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“如果道路上没有车道标记,您应该在道路的uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“天快黑了,太阳也渐渐暗了,你应该打开3号。”,
“在高速公路上开车之前,你应该确保你有足够的油、水和正确的轮胎压力。”]
答案1=[‘安全带’、‘左’、‘灯’、‘燃料’]
Answeed_list_1=[“开车时,务必系好安全带,包括乘客。”,
“如果道路上没有标记车道,则应在道路左侧行驶。”,
“天渐渐黑了,太阳也渐渐暗了,你应该开灯。”,
“在高速公路上行驶之前,你应该确保有足够的燃油、机油、水和正确的轮胎压力。”]
#中等模式
一般食物测试=[“所有水果中都含有1,这就是它们与蔬菜的区别所在。”,
“你睡觉的时候不应该喝咖啡或2,因为咖啡会让你晚上睡不着。”,
“未煮熟的食物不安全。它可能被一种名为沙门氏菌的细菌污染。”,
“制作蛋糕的基本要素是面粉、香草精、无盐黄油和鸡蛋。”]
答案2=[‘种子’、‘茶’、‘鸡肉’、‘糖’]
答案是[u list_2][“所有水果都有种子,这就是它们不同于蔬菜的原因。”,
“你睡觉的时候不应该喝咖啡或茶。这会让你晚上睡不着。”,
“吃未煮熟的鸡肉是不安全的。它可能被一种名为沙门氏菌的细菌污染。”,
“制作蛋糕的基本要素是面粉、糖、香草精、无盐黄油和鸡蛋。”]
#硬模式
一般健康测试=[“每天一次,医生远离你。”,
“过多的膳食摄入可能会对你有害。但是,这不太可能。”,
“你可以在手腕、3和大腿上检查脉搏。”,
“成年人平均需要7-9小时。”]
anwser_3=[‘苹果’、‘有害’、‘脖子’、‘睡眠’]
答案是[每天一个苹果,远离医生],
“过多的膳食摄入可能对你有害。但是,可能性不大。”,
“你可以检查手腕、脖子和大腿的脉搏。”,
“成年人平均需要7-9小时的睡眠。”]
空格=[“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
#输入原始输入的名称
#输出带有用户名的简介
def简介(球员姓名):
打印“Hello”+玩家姓名+”,我们将玩填空游戏!主题围绕我们的日常生活,如果你学习不多,不要担心。”
打印“请注意游戏区分大小写。即,必须禁用所有capslock。”
打印“让我们开始吧,选择一个困难。”
#返回用户对列表的选择
def游戏开始(玩家难度):
尽管如此:
如果玩家难度=“容易”:
打印“您选择了“简单”\n
返回驾驶员知识测试,回答1,回答列表1,使用返回功能
elif玩家难度==“中等”:
打印“您选择了“中等”\n
返回一般食物测试、答案2、答案列表2
elif玩家难度=“难”:
打印“您选择了“硬”\n
返回一般健康测试、答案3、答案列表3
其他:
打印“以下困难不存在。请重试!”
#检查用户输入的正确答案,并打印出他们的进度。如果用户输入不正确,则提示重试
def应答检查(应答、句子、空格、应答列表):
新列表=[]
索引=0
句子索引=0
答案(指数=0)
空白索引=0
已应答列表索引=0
句子总数=4#句子列表中的元素数。
打印句子[句子索引]
而索引<句子的总长度:
打印句子[句子索引]
player\u answer=raw\u input(“\n为“+空格[空格索引]+”\n>>”)键入您的答案)
如果玩家回答.lower()=回答[回答索引]:
#新增列表。追加(句子[句子索引])
新增列表。追加(已应答列表[已应答列表索引])
#请填写已回答的空白
打印句子[句子索引]。替换(空格[空格索引],答案[答案索引])
已应答列表索引+=1
答案(指数+=1)
句子索引+=1
指数+=1
空白索引+=1
打印“\n正确!\n”
其他:
打印“错误!重试”:\n
打印句子
return“\n祝贺您!您完成了游戏!”
#输入无
#输出整个程序功能
def在空格中填充()
玩家的姓名=原始输入(“输入您的姓名”\n>>)
简介(玩家姓名)
难度\u选择=原始输入(“\n从简单、中等或硬\n>>”中选择)
如果难度_选择==“容易”或
def fill_in_the_blanks():
    name_of_player = raw_input("enter your name \n>>>")
    Intro(name_of_player)

    difficulty_selection = ""

    while difficulty_selection not in ["easy", "medium", "hard"]:
        difficulty_selection = raw_input("\nselect from easy, medium, or hard \n>>>")

    sentence, answers, answered_sentence = beginning_of_game(difficulty_selection)
    print sentence

    answer_check(answers, sentence, blank_space, answered_sentence)