Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Me: Randomly select a programming language. In that language, in 50 lines or less, write a trivia game.
- #
- # AI: Here is a randomly-selected programming language: Python
- # And here is a trivia game written in Python in 50 lines or less: [code below]
- # This code defines a list of trivia questions and answers, shuffles the list,
- # and then presents each question to the user, allowing them to choose an answer
- # from a list of options. The code then checks the user's answer and increments
- # their score if it is correct. At the end, the code displays the user's final score.
- import random
- questions = [
- {
- "question": "What is the capital of France?",
- "answers": ["Paris", "London", "Berlin", "Rome"],
- "correct": "Paris"
- },
- {
- "question": "What is the largest planet in the solar system?",
- "answers": ["Earth", "Jupiter", "Saturn", "Neptune"],
- "correct": "Jupiter"
- },
- {
- "question": "Who invented the lightbulb?",
- "answers": ["Thomas Edison", "Nikola Tesla", "Benjamin Franklin", "Isaac Newton"],
- "correct": "Thomas Edison"
- },
- {
- "question": "What is the symbol for the element gold?",
- "answers": ["Au", "Ag", "Cu", "Fe"],
- "correct": "Au"
- },
- {
- "question": "What is the highest mountain in the world?",
- "answers": ["Mount Everest", "K2", "Kilimanjaro", "Denali"],
- "correct": "Mount Everest"
- }
- ]
- random.shuffle(questions)
- score = 0
- for question in questions:
- print(question["question"])
- for i, answer in enumerate(question["answers"]):
- print(f"{i + 1}: {answer}")
- user_input = input("Enter the number of your answer: ")
- if question["answers"][int(user_input) - 1] == question["correct"]:
- print("Correct!")
- score += 1
- else:
- print("Incorrect.")
- print(f"You scored {score} out of {len(questions)}.")
Advertisement
Add Comment
Please, Sign In to add comment