Python填充空白代码

Python填充空白代码,python,Python,我是一名编程初学者,我正在尝试建立一个填补空白的测验。我几乎完成了,但我被困在两个我无法解决的问题上,无论我做什么。我非常感谢你在这方面的帮助。谢谢你帮我做这件事 如果您尝试运行代码并玩游戏: 1它根据难度轻松疯狂和你想玩的游戏Apple、bond和编程测验打印测验,这很好,但之后它会提示你再次选择难度。即使玩家/用户已经选择了难度等级,玩家等级功能仍会继续运行。我真的不明白为什么会这样?在我看来,球员级别的程序似乎是完美的、合乎逻辑的 2.错误: 赋值前引用的局部变量blanks_索引 b未定

我是一名编程初学者,我正在尝试建立一个填补空白的测验。我几乎完成了,但我被困在两个我无法解决的问题上,无论我做什么。我非常感谢你在这方面的帮助。谢谢你帮我做这件事

如果您尝试运行代码并玩游戏:

1它根据难度轻松疯狂和你想玩的游戏Apple、bond和编程测验打印测验,这很好,但之后它会提示你再次选择难度。即使玩家/用户已经选择了难度等级,玩家等级功能仍会继续运行。我真的不明白为什么会这样?在我看来,球员级别的程序似乎是完美的、合乎逻辑的

2.错误: 赋值前引用的局部变量blanks_索引 b未定义答案的全局名称列表。 我知道它与initialize_game函数有关,但我不知道如何更改代码,因此它引用了所有变量blanks_index、answers_index、player_lived正确。 我想可以通过创建全局变量来解决这个问题,但这不是一个好的做法,所以我试图避免它。以前,整个函数initialise_game和play_game是一个函数,但由于一个函数中有25行以上的代码,这不是一个好的做法,因为它很长,很乱,我知道我可以将其分开,但我不知道如何分开

代码如下:

"""3 diffferent quizzes : Apple quiz, James Bond quiz, Programming quiz"""

"""Quiz and answers about Apple"""
Apple_quiz = ("The most valuable company in terms of market cap in 2016 is, ___1___."
              "It was founded in ___2___. Its flagship product is called ___3___."
              "___1___ has many competitors, the biggest rival is ___4___,founded by"
              " nobody but the richest man on the planet,___5___ ___6___.")

list_of_answers_Apple = ["Apple", "1976", "Iphone", "Microsoft", "Bill", "Gates"]


"""Quiz and answers about Bond"""
Bond_quiz = ("James Bond is agent ___1___. He serves his country,___2___ ___3___"
             " against its enemies. His car of choice is usually ___4___ ___5___."
             "  His favorite drink is ___6___.")

list_of_answers_Bond = ["007", "United", "Kingdom", "Aston", "Martin", "Martini"]

"""Quiz and answers about programming basics"""
Programming_quiz =  ("___1___ are created with the def keyword. ___1___ are also called ___2___"
                   " You specify the inputs a ___1___ take by adding ___3___ separated by commas"
                   " between the parentheses. ___3___ can be standard data types such as string, number"
                   " ,dictionary, tuple, and ___4___ or can be more complicated such as ___5___"
                   " and ___6___ functions.")

list_of_answers_Programming = ["Functions", "procedures", "arguments", "lists", "objects", "lambda"]

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


#List of levels with corresponding lives/guesses that player can have
quiz_list = ["Apple", "Bond", "Programming"]
level_list = ["easy", "medium", "hard", "superhard", "insane"]
lives_easy = 5
lives_medium = 4
lives_hard = 3
lives_superhard = 2
lives_insane = 1


def choose_quiz():
    """ Prompts player to pick a type of quiz and loads the quiz """
    #Input = player_quiz (raw input from player)
    #Output = loaded quiz, player chose
    while True:
        player_quiz = raw_input("Please, select a quiz you want to play: "
                          "(Apple, Bond or Programming): ")
        if player_quiz == "Apple":
            return Apple_quiz
        elif player_quiz == "Bond":
            return Bond_quiz
        elif player_quiz == "Programming":
            return Programming_quiz
        else:
            print "We don't have such quiz, pick again!"

def answers_for_quiz():
    """ Loads appropiate answers to the quiz that player has chosen"""
    #Input = player quiz (raw input from player)
    #Output = loaded quiz answers from the quiz player chose
    player_quiz_pick = choose_quiz()
    if player_quiz_pick == Apple_quiz:
        return list_of_answers_Apple
    elif player_quiz_pick == Bond_quiz:
        return list_of_answers_Bond
    elif player_quiz_pick == Programming_quiz:
        return list_of_answers_Programming

def player_level():
    """ Loads a difficulty that player chooses """
    #Input = player_level_input (raw input of player choosing a difficulty)
    #Output = corresponding number of lives:
    #Easy = 5 lives, Medium = 4 lives
    #Hard = 3 lives, Superhard = 2 lives
    #Insane = 1 life
    while True:
        player_level_input = raw_input("Please type in a difficulty level: "
                                 "(easy, medium, hard, superhard, insane): ")
        if player_level_input == "easy":
            return lives_easy #Easy = 5 lives
        elif player_level_input == "medium":
            return lives_medium #Medium = 4 lives
        elif player_level_input == "hard":
            return lives_hard #Hard = 3 lives
        elif player_level_input == "superhard":
            return lives_superhard #Superhard = 2 lives
        elif player_level_input == "insane":
            return lives_insane #Insane = 1 life
        else:
            print "We do not have such difficulty! Pick again!"

def correct_answer(player_answer, list_of_answers, answers_index):
    """ Checks, whether the the answer from player matches with the answer list. """
    #Input: player_answer (raw input that player enters in order to fill in the blank)
    #Output: "Right answer!" or "Wrong! Try again!" this output will be later used in the game
    if player_answer == list_of_answers[answers_index]:
        return "Right answer!"
    return "Wrong! Try again!"

def initialize_game():
    """Functions that sets up a game so we can play it """
    player_quiz_pick, player_level_pick, list_of_answers = choose_quiz(), player_level(), answers_for_quiz()
    print player_quiz_pick
    print "\nYou will get maximum " + str(player_level_pick) + " guesses for this game. Good luck.\n"
    blanks_index, answers_index, player_lives = 0, 0, 0

    #for elements in blank_space:
    while blanks_index < len(blank_space):
        player_answer = raw_input("Please type in your answer for " + blank_space[blanks_index] + ": ")
        if correct_answer(player_answer,list_of_answers,answers_index) == "Right answer!":
            print "Correct answer! Keep going!\n"
            player_quiz_pick = player_quiz_pick.replace(blank_space[blanks_index],player_answer)
            answers_index += 1
            blanks_index += 1
            print player_quiz_pick
            if blanks_index == len(blank_space):
                print "Congratulations! You nailed it! You are the winner!"
        else:
            player_level_pick -= 1
            if player_level_pick == 0:
                print "Game over! Maybe next time!"
                break
            else:
                print "One life less, that sucks! Have another shot!"
                print "You have " + str(player_level_pick) + " guesses left."

initialize_game()
您的主要问题是您不断地反复调用相同的函数,并且没有将输入保存到变量中。以下是有关代码和问题的一些提示:

您没有对您的player_level方法调用进行任何操作,因此玩家实际上不会以影响游戏的方式选择某个级别。您应该更改函数调用,以便存储返回的值

//the call to the method:
player_level_pick = player_level()
之后,继续调用player_level方法,而不使用用户提供的实际答案。将所有“玩家级别”外观更改为“玩家级别”\u pick-如上所示,用于保存答案的变量。所有其他不需要的函数调用(如choose_level)也是如此

您应该将猜数、玩家生命、答案列表和其他变量初始化为与玩家级别拾取匹配的值,以便根据级别保持正确的值。同样,您应该更改此行:

# the line that checks if game is over
# change from:
if number_of_guesses == player_lives:
# to :
if number_of_guesses == 0:
为了返回多个值,必须使用元组。一个接一个地使用多个return语句在任何地方都不起作用。 因此,不是:

return list_of_answers
return number_of_guesses
return blanks_index
return answers_index
return player_lives
您应该使用元组,并正确地将其解压缩:

# the return statement:
return (list_of_answers, number_of_guesses, blanks_index, answers_index, player_lives)

# and the unpacking in the calling function:
list_of_answers, number_of_guesses, blanks_index, answers_index, player_lives = initialize_game() 
这样,所有返回的值都进入调用函数中所需的变量。这样,您需要从play_game调用initialize_game。这将是你最有效的方法

再说一遍,正如我在第4章结尾所说的,你应该把初始化游戏和玩游戏组合成一个函数,因为很多数据都是相同的数据,或者从玩游戏中调用初始化游戏

更好的做法是递归地使用:return-choose\u-level,您应该使用while-True:loop,当您得到正确的答案时,只需刹车


Ofer Arial的回答中有很多好的建议。然而,实际上并没有必要将初始化游戏和玩游戏分开。因为你要把这么多的信息从一个传递到另一个,把它们分开有点混乱,这使得代码更难写和读。顺便说一句,你把列表中答案的末尾括号去掉了,因此,它不是调用函数&将结果列表命名为答案列表,而是将答案列表命名为函数的另一个名称。此外,您不应该在其末尾递归调用choose_测验,而应该使用while True循环。玩家级别也一样非常感谢,我实施了大部分更改,看起来还不错。我设法减少了玩家等级功能的重复,但选择测验功能仍然会重复一次。到目前为止,我还不知道为什么它会重复一次。我编辑了我的代码,所以现在您应该能够看到更改的代码。你介意看一下吗?谢谢不客气!你叫两次是什么意思?显示输出我设法解决了它,但谢谢你的询问。问题是,玩家被提示选择测验2次。当他被提示多次选择测验和难度等级时,仍然比以前更好。这是因为我没有在choose_测验函数中定义输入,而是在我的initialise_游戏和choose_测验函数中都将其指定为变量。因此,它被称为两次。