The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a trivia game in Python

Here’s a complete Python trivia game that runs in the console. It includes multiple-choice questions, input validation, scoring, and a simple way to add more questions:

python
def run_trivia_game(): questions = [ { "question": "What is the capital of France?", "options": ["A) Berlin", "B) Madrid", "C) Paris", "D) Rome"], "answer": "C" }, { "question": "Which planet is known as the Red Planet?", "options": ["A) Earth", "B) Mars", "C) Jupiter", "D) Venus"], "answer": "B" }, { "question": "Who wrote the play 'Romeo and Juliet'?", "options": ["A) Charles Dickens", "B) William Shakespeare", "C) Mark Twain", "D) Leo Tolstoy"], "answer": "B" }, { "question": "What is the largest ocean on Earth?", "options": ["A) Atlantic Ocean", "B) Indian Ocean", "C) Arctic Ocean", "D) Pacific Ocean"], "answer": "D" }, { "question": "Which element has the chemical symbol 'O'?", "options": ["A) Gold", "B) Oxygen", "C) Silver", "D) Osmium"], "answer": "B" } ] score = 0 print("Welcome to the Trivia Game! Answer the questions by typing A, B, C, or D.n") for idx, q in enumerate(questions, 1): print(f"Question {idx}: {q['question']}") for option in q["options"]: print(option) answer = input("Your answer: ").strip().upper() while answer not in ["A", "B", "C", "D"]: answer = input("Invalid input. Please enter A, B, C, or D: ").strip().upper() if answer == q["answer"]: print("Correct!n") score += 1 else: print(f"Wrong! The correct answer was {q['answer']}.n") print(f"Game Over! You scored {score} out of {len(questions)}.") if __name__ == "__main__": run_trivia_game()

Run this script in any Python 3 environment. It will ask 5 multiple-choice questions, track your score, and display the result at the end. You can add more questions by appending to the questions list.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About