Programming lesson
Build a Python Capital Quiz and Encryption Tool: A Step-by-Step Guide for 2026
Learn to create a Python state capital quiz game and a text encryption program using dictionaries, random selection, and while loops. Perfect for beginners in 2026.
Introduction: Why Python Dictionaries Are Your Best Friend in 2026
Python dictionaries are everywhere in modern programming—from powering AI recommendation systems to managing in-game inventories in the latest battle royale titles. In this tutorial, we'll build two practical projects: a state capital quiz and a text encryption tool. Both rely on dictionaries, random selection, and while loops. By the end, you'll have reusable code snippets that demonstrate key Python concepts while having fun with geography and cryptography.
Project 1: State Capital Quiz Game
Imagine you're a contestant on a 2026 geography-themed streaming show. The host randomly picks a state, and you must name its capital. Our Python program does exactly that—perfect for studying for a quiz or just testing your friends.
Step 1: Set Up the State-Capital Dictionary
We'll use a dictionary to map each state to its capital. For brevity, here's a partial list; you can expand it using the Wikipedia list of U.S. capitals.
state_capitals = {
'Alabama': 'Montgomery',
'Alaska': 'Juneau',
'Arizona': 'Phoenix',
'Arkansas': 'Little Rock',
'California': 'Sacramento',
# ... add all 50 states
}Step 2: Randomly Select a State
To randomize questions, we convert the dictionary keys to a list and use random.choice(). This is a common pattern when you need to pick a random key from a dictionary.
import random
states_list = list(state_capitals.keys())
random_state = random.choice(states_list)Step 3: Implement the Quiz Loop with a Sentinel
We'll use a while loop that continues until the user enters 'q' (the sentinel). Inside, we check the user's answer and track correct/incorrect counts.
correct = 0
incorrect = 0
while True:
random_state = random.choice(states_list)
answer = input(f"What is the capital of {random_state}? (or enter q to quit): ")
if answer.lower() == 'q':
break
if answer.strip().lower() == state_capitals[random_state].lower():
print("That is correct.")
correct += 1
else:
print(f"That is incorrect. The capital is {state_capitals[random_state]}.")
incorrect += 1
print(f"You had {correct} correct responses and {incorrect} incorrect responses.")This structure is similar to how AI chatbots handle user input—stay in a loop until a shutdown command is given.
Project 2: Text Encryption Using a Dictionary
Encryption is vital for protecting data, from secure messaging apps like Signal to blockchain transactions. Our simple substitution cipher replaces each character with a predefined mapping.
Step 1: Define the Encryption Dictionary
We'll use the provided dictionary (shortened for display). Each key is a plaintext character, and its value is the ciphertext character.
encrypt_code = {
'A': ')', 'a': '0', 'B': '(', 'b': '9',
'C': '*', 'c': '8', 'D': '&', 'd': '7',
# ... full dictionary from the assignment
}Step 2: Encrypt a Text File
We'll read a file, replace each character using the dictionary, and write the encrypted version to a new file. Characters not in the dictionary remain unchanged.
def encrypt_file(input_filename, output_filename):
with open(input_filename, 'r') as f:
content = f.read()
encrypted = ''.join(encrypt_code.get(char, char) for char in content)
with open(output_filename, 'w') as f:
f.write(encrypted)This technique is analogous to how early video game developers scrambled save files to prevent cheating.
Connecting to 2026 Trends
Both projects mirror real-world applications. The capital quiz is like the geography challenges in the latest GeoGuessr updates, while the encryption tool is a simplified version of what messaging apps use to protect your chats. Understanding dictionaries and loops is essential for building bots, analyzing data, or even creating your own AI.
Common Pitfalls and Tips
- Case sensitivity: Always normalize answers (e.g.,
.lower()) to avoid false negatives. - Sentinel placement: Check for the sentinel before evaluating the answer.
- Dictionary vs list: Remember, dictionaries don't support indexing by position. Use
list(dict.keys())for random access.
Next Steps
Expand the quiz to include world capitals or use the encryption tool to create a secret message for a friend. You could even combine both: encrypt a list of capitals!
By mastering these exercises, you're building a foundation for more advanced Python projects—whether that's a full-stack web app or a machine learning model. Happy coding in 2026!