无法定义名称“userInput”

2024-04-27 04:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在练习一些Python,我在尝试比较用户输入在hangman游戏中选择的单词时遇到了一些困难。在

# User guess input (single char)
# Exception handling to limit input to letters between a-z
while True:
    userInput = str(input('Please guess a letter:'))
    if len(userInput) == 1:
        if not re.match("^[a-z]*$", userInput):
            print("Error! Only letters a-z allowed!")
            continue
        break
    else:
        print("Error! Please input a single letter!")
        continue
# Comparing array with input
for i in range(len(selWord)):
    if(userInput == selWord[i]):

问题在于最后一行:

^{pr2}$

它声明“名称‘userInput’没有被定义。”但是当我试图在初始while循环之外打印时,它工作得很好。在


Tags: toinputleniferrorprintpleaseguess