Python刽子手游戏:如何在满足胜负条件时退出函数(替代break方案)
Hey there! The issue you're hitting with break makes total sense—break only works inside loops (like while or for loops). Right now, your code just calls hangman_start() twice manually, so there's no loop context for break to act on. Let's fix this with a couple of clean, practical approaches:
Option 1: Use a Main Game Loop (Most Elegant & Extensible)
Wrap your game logic in a while loop that runs until the player wins or loses. We'll have each round function return a boolean to tell the main loop whether to keep going:
displayed_word = [" ", " ", " ", " "] hangman = [":(", "-I", "-<", "you died!"] miss_count = 0 # Ditched the list workaround—an integer works perfectly here secret_word = ["t", "e", "s", "t"] def congratulations(): print("congratulations, you solved it") def play_round(): global displayed_word, miss_count guess = input("guess your letter: ") # Update displayed word for any matching letters (cleaner than 4 separate ifs) for idx, char in enumerate(secret_word): if guess == char: displayed_word[idx] = guess # Print the current state nicely (e.g., "t e t" instead of a raw list) print(' '.join(displayed_word)) # Check win condition if displayed_word == secret_word: congratulations() return False # Signal the game should end # Check wrong guess + lose condition if guess not in secret_word: miss_count += 1 print(hangman[miss_count]) if miss_count >= len(hangman) - 1: return False # Signal the game should end return True # Signal the game should continue # Main game loop—runs until win/lose game_active = True while game_active: game_active = play_round()
Quick Fixes in This Version:
- Removed the redundant redefinition of
secret_wordinside the function (it was overriding your global variable) - Simplified
miss_countto a regular integer (no need for the list hack) - Used
join()to make the displayed word look more user-friendly - The loop automatically handles game flow instead of manual function calls
Option 2: Use sys.exit() to Terminate Immediately
If you want a more direct way to quit when conditions are met, you can use sys.exit()—just remember to import the sys module first:
import sys displayed_word = [" ", " ", " ", " "] hangman = [":(", "-I", "-<", "you died!"] miss_count = -1 secret_word = ["t", "e", "s", "t"] def congratulations(): print("congratulations, you solved it") sys.exit() # Quit right after the win message def hangman_start(): global displayed_word, miss_count guess = input("guess your letter:") # Update displayed word for i in range(4): if guess == secret_word[i]: displayed_word[i] = guess print(displayed_word) if displayed_word == secret_word: congratulations() if guess not in secret_word: miss_count += 1 print(hangman[miss_count]) if miss_count == len(hangman)-1: sys.exit() # Quit right after the "you died!" message # Wrap in a loop to keep playing until win/lose while True: hangman_start()
This approach immediately stops the entire program when the win/lose condition is hit. It's simpler but less flexible if you want to add post-game logic (like asking to play again) later.
Key Takeaway:
You can’t use break without a loop—so either wrap your game in a loop (and control it with flags or return values) or use sys.exit() to terminate the program directly when your conditions are met.
内容的提问来源于stack exchange,提问作者Steve

