We’ll be creating a new script that parses a dictionary file into readable text. In the unit, we used the capitals of countries as our dictionary source. For this activity, we’ll be choosing and creating two new dictionaries to pull data from.

Step 1: Create Your Dictionaries

Choose two new themes for your new dictionaries. Here are a few examples:

Games and their developers
Top songs and their performing artists
The winners of a yearly tournament or competition

As a reminder, you can define a dictionary with the following code:

# creates a new dictionary
dictionaryName = {
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
"Key" : " Value",
}

Create a new file in PythonAnywhere. Add AT LEAST 10 keys and corresponding values to two different dictionaries.

Add comments to your code to explain what each line or section of your code is doing.

1 answer

Sure! Below is an example of a Python script that creates two dictionaries: one for video games and their developers, and another for popular movies and their directors. Each dictionary contains at least 10 keys (video games or movies) and their corresponding values (developers or directors). I've also included comments to explain what each part of the code does.

# Creates a dictionary for video games and their developers
games_dictionary = {
    "The Legend of Zelda: Breath of the Wild": "Nintendo",
    "The Witcher 3: Wild Hunt": "CD Projekt Red",
    "Dark Souls": "FromSoftware",
    "God of War": "Santa Monica Studio",
    "Minecraft": "Mojang Studios",
    "Fortnite": "Epic Games",
    "Red Dead Redemption 2": "Rockstar Games",
    "Hollow Knight": "Team Cherry",
    "Overwatch": "Blizzard Entertainment",
    "Celeste": "Maddy Makes Games"
}

# Creates a dictionary for popular movies and their directors
movies_dictionary = {
    "Inception": "Christopher Nolan",
    "The Shawshank Redemption": "Frank Darabont",
    "Pulp Fiction": "Quentin Tarantino",
    "The Godfather": "Francis Ford Coppola",
    "The Dark Knight": "Christopher Nolan",
    "Schindler's List": "Steven Spielberg",
    "Fight Club": "David Fincher",
    "Forrest Gump": "Robert Zemeckis",
    "The Matrix": "Lana and Lilly Wachowski",
    "Parasite": "Bong Joon-ho"
}

# Print the content of the games dictionary in a readable format
print("Video Games and their Developers:")
for game, developer in games_dictionary.items():
    print(f"{game} was developed by {developer}")

# Print the content of the movies dictionary in a readable format
print("\nPopular Movies and their Directors:")
for movie, director in movies_dictionary.items():
    print(f"{movie} was directed by {director}")

Explanation of the Code

  1. Dictionary Creation:

    • We create two dictionaries named games_dictionary and movies_dictionary. Each dictionary contains key-value pairs where the key is the name of a game or movie, and the value is its developer or director, respectively.
  2. Comments:

    • There are comments throughout the code explaining the purpose of each section, making it easier for anyone reading the code to understand its functionality.
  3. Looping through Dictionaries:

    • We use a for loop to iterate over each dictionary. The items() method returns key-value pairs, which we unpack into the variables game and developer or movie and director.
  4. Formatted Output:

    • In the print statements inside the loops, we use formatted string literals (f-strings) to neatly output the information in a reader-friendly way.

You can run this script in your Python environment to see the output, which will list the games with their developers and the movies with their directors in a clear format.