Description
The botTurn function in games/Word-Building/word_building.py contains an infinite loop bug. When all words for a given starting letter have already been used by both players, the while True loop never exits because there are no unused words to select.
Root Cause
The bare except: pass on line 20 only catches the KeyError when a letter doesn't exist in the word list. However, when the letter IS in the word list but ALL words for that letter are in used_words, the loop runs forever because:
random.randint picks a word from data.words[letter]
- The word IS in
used_words, so the break is never reached
- The loop repeats indefinitely with no exit condition
Impact
The game becomes permanently stuck when the bot runs out of words for a given letter. The user must force-kill the process. This is a runtime crash risk that makes the game unplayable after enough rounds.
Reproduction
- Play Word Building until many words have been used
- When the bot gets a letter where all words are exhausted, the game hangs forever
Expected Behavior
The function should return None (or handle the case) when no unused words remain for the letter.
Actual Behavior
Infinite loop with no exit condition.
Proposed Fix
Add a maximum iteration count or check if all words are used:
def botTurn(letter, used_words):
import data
word = None
try:
available = [w for w in data.words.get(letter, []) if w not in used_words]
if available:
word = random.choice(available)
except:
pass
return word
Description
The
botTurnfunction ingames/Word-Building/word_building.pycontains an infinite loop bug. When all words for a given starting letter have already been used by both players, thewhile Trueloop never exits because there are no unused words to select.Root Cause
The bare
except: passon line 20 only catches theKeyErrorwhen a letter doesn't exist in the word list. However, when the letter IS in the word list but ALL words for that letter are inused_words, the loop runs forever because:random.randintpicks a word fromdata.words[letter]used_words, so thebreakis never reachedImpact
The game becomes permanently stuck when the bot runs out of words for a given letter. The user must force-kill the process. This is a runtime crash risk that makes the game unplayable after enough rounds.
Reproduction
Expected Behavior
The function should return
None(or handle the case) when no unused words remain for the letter.Actual Behavior
Infinite loop with no exit condition.
Proposed Fix
Add a maximum iteration count or check if all words are used: