2017-02-11 228 views
-1

我是一名編程初學者,我正在嘗試構建一個填空題測驗。我差不多完成了,但我無法解決2個我無法解決的問題,無論我做什麼。我真的很感謝你的幫助。感謝您幫助我!Python填充空白代碼

如果您嘗試運行的代碼和玩遊戲:

1)根據你要玩(蘋果,債券和編程測驗的難度(易瘋狂)和測驗打印測驗),這雖然玩家/用戶已經選擇了難度級別,但是我仍然不明白爲什麼它會這樣做?player_level()過程看起來很完美

2)錯誤: a)局部變量blanks_index在賦值前引用 b)全局名稱list_of_answers爲 沒有定義的。 我知道它與initialize_game()函數有關,但我不知道如何更改代碼,以便正確引用所有變量(blanks_index,answers_index,player_lives)。 它可以通過創建全局變量來解決(我猜),但這不是一個好習慣,所以我試圖避免它。以前,整個函數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() 
+1

在Ofer Arial的回答中有很多很好的建議。但是,真的沒有必要分開'initialize_game'和'play_game'。因爲你將很多信息從一個傳遞到另一個,所以將它們分開有點麻煩,這使得代碼難以書寫和閱讀。 –

+0

順便說一句,您在'list_of_answers = answers_for_quiz'中將'answers_for_quiz'的末尾括起來,所以不是調用函數並命名結果'list_of_answers',而是將'list_of_answers'變成函數的另一個名稱。另外,你不應該在本身的末尾遞歸調用'choose_quiz()',而應該使用'while True'循環。 'player_level()'也一樣# –

回答

1

你的主要問題你保持一遍又一遍調用相同的功能和不輸入保存到變量。以下是關於你的代碼和問題的一些提示:

  1. 你不這樣做你player_level()方法調用任何東西,所以玩家實際上沒有影響比賽的方式選擇一個級別。你應該改變函數調用,所以返回的值將被存儲。

    //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一樣,所以它會根據級別按住右值。同樣,你應該改變這一行:

    # 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語句無法在任何地方工作。 這樣,而不是:

    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()。這將是你的有效途徑。

  5. 只需再這麼說了,因爲我在年底說(4) - 你應該單元initialize_game()play_game()成一個單一的功能(因爲很多數據是相同的所需的數據),或者只是叫initialize_game()play_game() 。然後

  6. 更好的做法,使用這個rec​​ursivly:return choose_level(),當你得到一個正確的答案,你應該使用一個while True:循環,只是剎車。

+0

非常感謝,我實現了大部分更改,看起來沒問題。我設法減少player_level()函數的重複,但choose_quiz()函數仍然重複一次。到目前爲止,我還沒有找到爲什麼它會重演一次。我編輯了我的代碼,所以現在你應該能夠看到更改後的代碼。你介意看看它嗎?謝謝! – Kronas

+0

不客氣!你打兩次電話是什麼意思?顯示輸出 –

+0

我設法解決它,但謝謝你的提問。問題在於玩家被提示選擇2次測驗(還是比以前更好地提示他多次選擇測驗和難度級別)。這是因爲我沒有在choose_quiz()函數中定義輸入,並將它作爲變量在我的initialise_game()和choose_quiz()函數中分配。因此,它被稱爲兩次。 – Kronas