Python终端猜词游戏嵌套while循环异常跳过问题求助
终端猜词游戏循环异常问题
我正在开发一款终端猜词游戏,游戏通过外层while循环要求玩家猜测指定长度的单词(实际字母数为变量diff+2)。但运行代码时,程序会跳过外层while循环及内部嵌套的while循环,直接执行嵌套循环后调用type()函数询问是否继续游戏的代码。变量stop、diff、guess均已在开头定义,使用Python 3.12.1版本。我尝试修改while循环的格式(从while stop != 'y'改为while stop not in ['y'])并调整缩进,但问题仍未解决,期望程序能完整执行外层循环直至玩家猜对单词。
问题代码片段
while stop not in ['y']: roll() if "'" in word: roll() print(word) while word != guess: type("\nYour guess...") guess = input(" ") guess = guess.lower() if len(guess) != diff + 2: type(f'\nYour guess has to be at least {diff+2} characters long.') for i in range(len(guess)): if guess[i] == word[i]: print(f"{bcolors.OKGREEN}{guess[i]}{bcolors.ENDC}", end = '') elif guess[i] in word: print(f"{bcolors.WARNING}{guess[i]}{bcolors.ENDC}", end = '') else: print(f"{bcolors.FAIL}{guess[i]}{bcolors.ENDC}", end = '') type('\nWould you like to play again or stop? (a/s)') ans = input(" ") if ans == 's': stop = 'y'
完整代码
import nltk from nltk.corpus import brown from time import sleep from os import * from random import * import sys import os.path def type(phrase, speed = 1): a = 0 for i in range (len(phrase)): print(phrase[a], end='', flush=True) c = phrase[a] if c == '.' or c=='!' or c=='?': sleep(0.5/speed) elif c == ',': sleep(0.25/speed) else: sleep(0.05/speed) a+=1 word = '' diff = '' stop = '' guess = '' def roll(): word = (word_list[randint(0, len(word_list)-1)]) word = word.lower() nltk.download('brown') common_words = set(brown.words()) common_known_words = sorted(set(common_words)) threelw = [word for word in common_known_words if len(word) == 3] fourlw = [word for word in common_known_words if len(word) == 4] fivelw = [word for word in common_known_words if len(word) == 5] sixlw = [word for word in common_known_words if len(word) == 6] class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKCYAN = '\033[96m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' system('cls') type(f"Welcome to...") type(''' ░▒▓█▓▒░ ░▒▓█▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒▒▓███▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓████████▓▒░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓██████▓▒░ ''', 45) print("\n") sleep(1) type("The game where you get a word and you have to guess it in as little guesses possible.") type('''\nWhat difficulty would you like to play at?:\n[1] 3 letter words\n[2] 4 letter words\n[3] 5 letter words\n[4] 6 letter words\n''') while diff not in ['1', '2', '3', '4', '5']: diff = input("\n(1/2/3/4): ") if diff == '1': word_list = threelw elif diff == '2': word_list = fourlw elif diff == '3': word_list = fivelw elif diff == '4': word_list = sixlw else: type("Invalid input. Try again.") diff = int(diff) while stop not in ['y']: roll() if "'" in word: roll() print(word) while word != guess: type("\nYour guess...") guess = input(" ") guess = guess.lower() if len(guess) != diff + 2: type(f'\nYour guess has to be at least {diff+2} characters long.') for i in range(len(guess)): if guess[i] == word[i]: print(f"{bcolors.OKGREEN}{guess[i]}{bcolors.ENDC}", end = '') elif guess[i] in word: print(f"{bcolors.WARNING}{guess[i]}{bcolors.ENDC}", end = '') else: print(f"{bcolors.FAIL}{guess[i]}{bcolors.ENDC}", end = '') type('\nWould you like to play again or stop? (a/s)') ans = input(" ") if ans == 's': stop = 'y'
问题原因及修复方案
1. 全局变量未被正确修改
roll()函数内的word是局部变量,无法改变全局定义的word。初始状态下全局word和guess都是空字符串,导致内层while word != guess循环直接跳过。
修复:在roll()函数内声明word为全局变量:
def roll(): global word word = (word_list[randint(0, len(word_list)-1)]) word = word.lower()
2. 每轮游戏未重置guess
玩家选择再玩一次时,guess保留上一轮猜对的值,会直接跳过内层猜词循环。
修复:在外层循环开始时重置guess:
while stop not in ['y']: guess = '' # 重置猜词变量 roll() # 后续代码不变
3. 带单引号单词的检测逻辑不完善
当前仅重新抽词一次,仍可能抽到带单引号的单词,需循环检测直到抽到符合要求的单词:
while stop not in ['y']: guess = '' roll() while "'" in word: # 循环抽词直到无单引号 roll() print(word) # 后续代码不变
4. 输入长度错误时避免索引异常
当玩家输入长度不符合要求时,仍会执行字符对比循环,可能引发索引错误。需跳过后续判断:
while word != guess: type("\nYour guess...") guess = input(" ") guess = guess.lower() if len(guess) != diff + 2: type(f'\nYour guess has to be exactly {diff+2} characters long.') continue # 跳过字符对比环节 for i in range(len(guess)): if guess[i] == word[i]: print(f"{bcolors.OKGREEN}{guess[i]}{bcolors.ENDC}", end = '') elif guess[i] in word: print(f"{bcolors.WARNING}{guess[i]}{bcolors.ENDC}", end = '') else: print(f"{bcolors.FAIL}{guess[i]}{bcolors.ENDC}", end = '') print() # 添加换行优化输出格式
内容的提问来源于stack exchange,提问作者JJ Does Python
相关产品推荐
相关产品推荐

