Skip to content

🐛 [Bug]: Word-Building game crashes with KeyError when user word starts with unmapped letter #1315

Description

@vipul674

Summary

The Word-Building game (games/Word-Building/word_building.py line 56) crashes with a KeyError when the user enters a word whose first letter is not a key in the data.words dictionary. The code accesses data.words[user_word[0]] without checking whether that key exists.

Evidence

In games/Word-Building/word_building.py:56:

if user_word not in data.words[user_word[0]]:
    data.DataAdding(user_word)

If the user enters a word like "zebra" and the letter "z" is not a key in the data.words dictionary (loaded from words.bat), this line raises:

KeyError: z

The data.words dictionary is loaded from a pickle file (words.bat) and may not contain entries for all 26 letters of the alphabet.

Note: This is distinct from issue #1204 which reports a similar KeyError in the DataAdding() function in data.py. This bug is in the Main() function in word_building.py.

Affected Files

  • games/Word-Building/word_building.py (line 56)

Reproduction Steps

  1. Run python word_building.py
  2. Play through the first round (bot starts)
  3. Enter a word that starts with a letter not present in the data.words dictionary
  4. The game crashes with KeyError

Expected Behavior

The game should handle missing keys gracefully — either by adding the key to the dictionary or by displaying an error message and prompting for a different word.

Actual Behavior

The game crashes with an unhandled KeyError traceback.

Impact

The game crashes when users enter words with uncommon starting letters. This makes the game fragile and limits the vocabulary users can play with.

Suggested Fix

Use .get() with a default empty list, or check for key existence:

if user_word[0] not in data.words or user_word not in data.words[user_word[0]]:
    data.DataAdding(user_word)

Or in data.pys DataAdding function, initialize missing keys:

def DataAdding(word):
    # ...existing code...
    if word[0] not in words:
        words[word[0]] = []
    # ...rest of code...

Candidate Validation

GSSoC 2026

This is a valid bug fix suitable for GSSoC 2026 contributors. The fix involves defensive programming with dictionary key checks, which is a fundamental Python skill.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions