使用Python的KBC遊戲
簡介
一個著名的印度遊戲節目,叫做百萬富翁 (Kaun Banega Crorepati, KBC),是根據國際遊戲節目“誰想成為百萬富翁”改編的。在這篇文章中,我們將瞭解如何使用Python程式語言來製作一個簡化的KBC遊戲版本。
KBC
定義
在KBC遊戲中,參賽者選擇正確答案來回答多項選擇題,以贏得獎金,獎金隨著每次正確回答而增加。遊戲的目標是從一系列包含多個選項的問題中選擇正確的答案。每個正確答案都會獲得一定金額的獎金,而錯誤答案則會導致遊戲結束。
語法
變數 −
question - Stores the question text. options - A list that contains multiple-choice options. answer - Stores the index of the correct answer.
使用者輸入 −
input() - Reads user input from the console.
條件語句 −
if - Checks a condition and executes a block of code if the condition is true. else - Executes a block of code if the preceding if statement condition is false.
迴圈 −
for loop - Iterates over a sequence of elements. while loop - Repeats a block of code until a condition is met.
在KBC遊戲中,需要變數來儲存問題、可能的答案和正確答案。為了顯示問題和接收使用者輸入,這些變數至關重要。
使用者輸入 − 我們使用`input()`函式從控制檯讀取使用者輸入。然後,玩家可以透過輸入相應的選項編號來選擇他們的答案。
條件語句 − 為了比較使用者的答案和正確答案,我們使用`if`和`else`之類的條件語句。如果使用者的答案正確,則使用者獲得獎金。否則,遊戲結束。
使用迴圈,我們可以遍歷問題列表,一次向玩家提出一個問題。當我們希望遊戲持續到滿足某個條件時,`while`迴圈比較有用;而當我們有固定數量的問題時,`for`迴圈則更有效。
演算法
步驟1 − 設定初始獎金金額。
步驟2 − 建立一個包含問題、選項和正確答案的列表。
步驟3 − 遍歷問題列表。
步驟4 − 顯示所有問題和答案。
步驟5 − 獲取使用者答案。
步驟6 − 檢查使用者答案是否與正確答案匹配。
步驟7 − 根據需要調整獎金金額。
步驟8 − 如果使用者答案錯誤,遊戲結束。
步驟9 − 重複步驟3,直到所有問題都已解答或遊戲結束。
步驟10 − 顯示玩家最終獲得的獎金金額。
方法
方法1 − 使用`for`迴圈。
方法2 − 使用`while`迴圈
方法1:使用for迴圈
示例
# Importing the random module to shuffle questions import random # Set the initial amount of prize money prize_money = 0 # Create a list of questions with options and answers questions = [{ "question": "What is the capital of India?", "options": ["1. Delhi", "2. Mumbai", "3. Kolkata", "4. Chennai"], "answer": 1 },{ "question": "Which planet is known as the Red Planet?", "options": ["1. Jupiter", "2. Venus", "3. Mars", "4. Saturn"], "answer": 3 },{ "question": "Who painted the Mona Lisa?", "options": ["1. Vincent van Gogh", "2. Pablo Picasso", "3. Leonardo da Vinci", "4. Claude Monet"], "answer": 3 }] # Shuffle the questions random.shuffle(questions) # Iterate over the questions using a for loop for question in questions: # Display the question and options print(question["question"]) for option in question["options"]: print(option) # Read the user's answer user_answer = int(input("Enter your answer (option number): ")) # Check if the user's answer is correct if user_answer == question["answer"]: print("Correct answer!") prize_money += 1000 else: print("Wrong answer!") break # End the game if the answer is incorrect # Display the final prize money earned by the player print("Prize Money:", prize_money)
輸出
What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (option number): 1 Correct answer! Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (option number): 2 Wrong answer! Prize Money: 1000
`random.shuffle()`函式用於在每次遊戲時以不同的順序打亂問題。
方法2:使用while迴圈
示例
# Set the initial amount of prize money prize_money = 0 # Create a list of questions with options and answers questions = [{ "question": "What is the capital of India?", "options": ["1. Delhi", "2. Mumbai", "3. Kolkata", "4. Chennai"], "answer": 1 },{ "question": "Which planet is known as the Red Planet?", "options": ["1. Jupiter", "2. Venus", "3. Mars", "4. Saturn"], "answer": 3 },{ "question": "Who painted the Mona Lisa?", "options": ["1. Vincent van Gogh", "2. Pablo Picasso", "3. Leonardo da Vinci", "4. Claude Monet"], "answer": 3 }] # Set the initial question index question_index = 0 # Set the initial game status game_over = False # Game loop while not game_over: # Get the current question question = questions[question_index] # Display the question and options print(question["question"]) for option in question["options"]: print(option) # Read the user's answer user_answer = int(input("Enter your answer (option number): ")) # Check if the user's answer is correct if user_answer == question["answer"]: print("Correct answer!") prize_money += 1000 else: print("Wrong answer!") game_over = True # End the game if the answer is incorrect # Move to the next question question_index += 1 # Check if all questions have been answered if question_index == len(questions): game_over = True # Display the final prize money earned by the player print("Prize Money:", prize_money)
輸出
What is the capital of India? 1. Delhi 2. Mumbai 3. Kolkata 4. Chennai Enter your answer (option number):1 Correct answer! Which planet is known as the Red Planet? 1. Jupiter 2. Venus 3. Mars 4. Saturn Enter your answer (option number): 3 Correct answer! Who painted the Mona Lisa? 1.Vincent van Gogh 2. Pablo Picasso 3. Leonardo da Vinci 4. Claude Monet Correct answer! Prize Money: 3000
使用`while`迴圈建立遊戲迴圈,該迴圈持續執行直到遊戲結束。如果使用者給出錯誤答案或所有問題都已回答,則將遊戲結束條件設定為`True`。
結論
總之,這是一個強大且靈活的基礎,可以用來開發一個有趣的問答遊戲,讓玩家在娛樂和引人入勝的方式中測試他們的知識並贏取獎金。