2017-02-11 8 views
-1

私はプログラミングの初心者です。空白記入のクイズを作成しようとしています。私はほとんど終わりましたが、私が何をしても解決できない2つの問題に悩まされています。私は本当にこれであなたの助けに感謝します。これを手伝ってくれてありがとう!Python記入空白コード

あなたは、コードを実行し、ゲームをプレイしてみた場合:

1)それはあなたが(りんご、債券およびプログラミングのクイズをプレイしたい難易度(イージー非常識)とクイズに応じてクイズを出力)がしかし、後で難易度を選択するように促します(player_level()関数は、難易度レベルを選んだプレイヤー/ユーザーがいても継続しています)大文字小文字の区別がありません。

2)エラー: a)割り当て前に参照されているローカル変数blanks_index b)グローバル名list_of_answers is 定義されていません。 私はinitialize_game()関数に関連していることを知っていますが、すべての変数(blanks_index、answers_index、player_lives)を正しく参照するようにコードを変更する方法はわかりません。 グローバル変数を作成することで解決できるかもしれませんが、それは良い習慣ではありませんので、避けようとしています。以前は、関数initialise_game()とplay_game()は1つの関数でしたが、1つの関数に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() 
+1

Ofer Arialの回答には、十分なアドバイスがあります。しかし、 'initialize_game'と' play_game'を分離する必要はありません。多くの情報を1つから別のものに渡すので、それらを分離するのはちょっと面倒なので、コードの書き込みと読み込みが難しくなります。 –

+0

ところで、あなたは 'list_of_answers = answers_for_quiz'の' answers_for_quiz'の最後にかっこを残しておいたので、関数を呼び出す代わりに 'list_of_answers'という名前をつけて、' list_of_answers'を関数の別の名前にしました。また、自身の最後に 'choose_quiz()'を再帰的に呼び出すのではなく、 'while True'ループを使用してください。同じことが 'player_level()'になります –

答えて

1

あなたの主な問題あなたは何度も何度も同じ機能を呼び出しておくと変数に入力を保存しないということである。ここでは

は、コードがあります。プレイヤーが実際にゲームに影響を与える方法でレベルを選択しないように、あなたはあなたのplayer_level()メソッドの呼び出しで何もしていません

  1. :はここにあなたのコードとの質問についていくつかのヒントがあります。関数呼び出しを変更して、返された値が格納されるようにしてください。

    //the call to the method: 
    player_level_pick = player_level() 
    
  2. その後、あなたはplayer_level()メソッドを呼び出して、ユーザーが供給していること、実際の答えを使用していない続けます。 player_level()の回答をplayer_level_pickに変更してください(上記のように、回答を保存するために使用する変数)。 choose_level()のような他の不要な関数呼び出しにも同じです。

  3. number_of_guesses, player_lives, list_of_answersとその他の変数の一致値をplayer_level_pickに設定する必要があります。レベルに応じて適切な値が保持されます。同様に、あなたはこの行を変更する必要がありますreturn複数の値にするために

    # the line that checks if game is over 
    # change from: 
    if number_of_guesses == player_lives: 
    # to : 
    if number_of_guesses == 0: 
    
  4. を、あなたはタプルを使用する必要があります。複数の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() 
    

    このように、返される値の全ては、呼び出し元の関数で希望の変数に入ります。この方法でinitialize_game()play_game()から呼び出す必要があります。それはあなたのための効率的な方法になります。 (データの多くは、同じ必要なデータであるため)、またはあなたは、単一の機能にユニットinitialize_game()play_game()だけplay_game()からinitialize_game()を呼び出す必要があります - 私は(4)の最後に言ったよう

  5. ただ、再びそれを言って。

  6. この再帰的な使用:return choose_level()を使用すると、while True:ループを使用し、適切な回答が得られたらブレーキをかけてください。

+0

ありがとう、私はほとんどの変更を実装しました。大丈夫です。私はplayer_level()関数の繰り返しを減らすことができましたが、choose_quiz()関数はそれ自身を一度繰り返します。これまでのところ、私はそれがなぜ一度繰り返されるのかを知ることができませんでした。私は自分のコードを編集したので、変更されたコードを見ることができるはずです。それを見てもらえますか?ありがとう! – Kronas

+0

あなたは大歓迎です!二度呼ばれたことはどういう意味ですか?出力を表示する –

+0

私はそれを解決することができたが、尋ねてくれてありがとう。問題は、プレイヤーがクイズを2回選択するよう促されたことです(クイズと難易度の両方を選択するように何度も促されていた以前よりも優れていました)。これは、choose_quiz()関数内で入力を定義していないため、initialise_game()およびchoose_quiz()関数の両方で変数として割り当てられていたためです。したがって、それは2度呼ばれた。 – Kronas

関連する問題