You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Python程序无终端输出且CPU使用率异常飙升

Troubleshooting Your Hangman Game: No Output & High CPU Usage

Hey there! Let's break down what's causing your issues and fix them step by step.

1. The Root Cause of High CPU & No Output: Infinite Loop in get_valid_word

Your get_valid_word function has a flawed loop condition that's trapping it in an endless cycle:

while '-' in word or '' in word: #筛选掉包含连字符或空格的单词

The empty string '' is a substring of every possible string—no matter which word the random picker selects, this condition will always be True. That means your program is stuck here forever, never getting to the print statements or input prompts later in the code. No wonder your CPU is spiking and you see no output!

Fix It:

You intended to filter out words with spaces, so replace the empty string '' with a space character ' ':

while '-' in word or ' ' in word: #筛选掉包含连字符或空格的单词

2. Your Game Never Starts: Missing Function Call

Right now, you've defined the hangman() function, but you never actually run it. Even after fixing the loop, your program will just define functions and exit without doing anything.

Fix It:

Add this line at the very end of your code to launch the game:

hangman()

3. Bonus: Infinite Game Restarts

Once a player guesses the full word, your code calls hangman() again, which restarts the game indefinitely. If you want the game to end after a win, replace that recursive call with a victory message:

# Replace hangman() with this:
print(f"Great job! You guessed the word: {word}")

Putting all these fixes together will get your hangman game running properly—you'll see the prompts, input letters, and CPU usage will go back to normal.

内容的提问来源于stack exchange,提问作者poiyti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 20:02:29