Today you combine everything you’ve learned:
By the end, you will have a small game that you can expand.
questions = {
"What is the capital of Germany?": "Berlin",
"2+2 = ?": "4",
"Is Python a programming language?": "Yes"
}
points = 0
for question in questions:
answer = input(question + " ")
if answer == questions[question]:
points += 1
print("Correct!")
else:
print("Wrong!")
print("You scored", points, "out of", len(questions), "points!")
Explanation:
questions stores the questions and answers
answer = input(question + " ").lower()
if answer == questions[question].lower():
👉 .lower() converts everything to lowercase → avoids errors with uppercase/lowercase
quiz = {
"Math": {
"2+2 = ?": "4",
"5*3 = ?": "15"
},
"General": {
"Capital of Germany?": "Berlin"
}
}
category = input("Choose a category: ")
questions = quiz[category]
quiz = {
"easy": {
"2+2": "4"
},
"hard": {
"12*12": "144"
}
}
level = input("Choose difficulty (easy/hard): ")
questions = quiz[level]
file = open("results.txt", "a")
file.write("Points: " + str(points) + "\n")
file.close()
import random
quiz = {
"easy": {
"2+2": "4",
"3+5": "8"
},
"hard": {
"12*12": "144",
"15*3": "45"
}
}
level = input("Choose difficulty (easy/hard): ")
questions = quiz[level]
points = 0
for question in questions:
answer = input(question + " ").lower()
if answer == questions[question].lower():
points += 1
print("Correct!")
else:
print("Wrong!")
print("You scored", points, "out of", len(questions), "points!")
# Save results
file = open("results.txt", "a")
file.write("Level: " + level + " Points: " + str(points) + "\n")
file.close()
Example for randomness:
import random
question = random.choice(list(questions.keys()))