Farrier

Python trivia game, written by AI.

Dec 25th, 2022
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | Gaming | 0 0
  1. # Me: Randomly select a programming language. In that language, in 50 lines or less, write a trivia game.
  2. #
  3. # AI: Here is a randomly-selected programming language: Python
  4. # And here is a trivia game written in Python in 50 lines or less: [code below]
  5. # This code defines a list of trivia questions and answers, shuffles the list,
  6. # and then presents each question to the user, allowing them to choose an answer
  7. # from a list of options. The code then checks the user's answer and increments
  8. # their score if it is correct. At the end, the code displays the user's final score.
  9.  
  10. import random
  11.  
  12. questions = [
  13.     {
  14.         "question": "What is the capital of France?",
  15.         "answers": ["Paris", "London", "Berlin", "Rome"],
  16.         "correct": "Paris"
  17.     },
  18.     {
  19.         "question": "What is the largest planet in the solar system?",
  20.         "answers": ["Earth", "Jupiter", "Saturn", "Neptune"],
  21.         "correct": "Jupiter"
  22.     },
  23.     {
  24.         "question": "Who invented the lightbulb?",
  25.         "answers": ["Thomas Edison", "Nikola Tesla", "Benjamin Franklin", "Isaac Newton"],
  26.         "correct": "Thomas Edison"
  27.     },
  28.     {
  29.         "question": "What is the symbol for the element gold?",
  30.         "answers": ["Au", "Ag", "Cu", "Fe"],
  31.         "correct": "Au"
  32.     },
  33.     {
  34.         "question": "What is the highest mountain in the world?",
  35.         "answers": ["Mount Everest", "K2", "Kilimanjaro", "Denali"],
  36.         "correct": "Mount Everest"
  37.     }
  38. ]
  39.  
  40. random.shuffle(questions)
  41.  
  42. score = 0
  43. for question in questions:
  44.     print(question["question"])
  45.     for i, answer in enumerate(question["answers"]):
  46.         print(f"{i + 1}: {answer}")
  47.     user_input = input("Enter the number of your answer: ")
  48.     if question["answers"][int(user_input) - 1] == question["correct"]:
  49.         print("Correct!")
  50.         score += 1
  51.     else:
  52.         print("Incorrect.")
  53.  
  54. print(f"You scored {score} out of {len(questions)}.")
Advertisement
Add Comment
Please, Sign In to add comment