-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriviaAPI.py
More file actions
67 lines (52 loc) · 2.01 KB
/
triviaAPI.py
File metadata and controls
67 lines (52 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
import random
import html
# Education-focused categories (General Knowldege, Science, History, etc.)
EDUCATION_CATEGORY_ID = 9 # General Knowledge category (most educational)
API_URL = f"https://opentdb.com/api.php?amount=10&category={EDUCATION_CATEGORY_ID}&type=multiple"
def get_education_questions():
response = requests.get(API_URL)
if response.status_code == 200:
data= response.json()
if data['response_code'] == 0 and data['results']:
return data['results']
return None
def run_quiz():
questions = get_education_questions()
if not questions:
print("Failed to fetch educational questions.")
print("Failed to retrieve questions.")
return
score = 0
print("Welcome to the Educational Quiz!\n")
for i, q in enumerate(questions, 1):
# Decode HTML entities and prepare options
question = html.unescape(q['question'])
correct = html.unescape(q['correct_answer'])
incorrect = [html.unescape(a) for a in q['incorrect_answers']]
# Create and shuffle options
options = incorrect + [correct]
random.shuffle(options)
# Display questions
print(f"Question {i}: {question}")
for idx, option in enumerate(options, 1):
print(f" {idx}.{option}")
# Get and validate answer
while True:
try:
choice = int(input("\nYour Answer (1-4):"))
if 1 <= choice <= 4:
break
except ValueError:
pass
print("Invalid input! Please enter a number between 1 and 4.")
# Check answer
if options[choice-1] == correct:
print("✓ Correct!\n")
score +=1
else:
print(f"✗ Wrong! The correct answer was: {correct}\n")
print(f"Final Score: {score}/{len(questions)}")
print(f'Percentage: {score/len(questions)*100:.1f}%')
if __name__ == "__main__":
run_quiz()