python - 无法定义名称 "userInput"

标签 python python-3.x

我正在练习一些 Python,我在尝试比较 userInput 将在刽子手游戏中选择的单词时遇到了一些麻烦。

# 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]):

问题出在最后一行:

        if(userInput == selWord[i]):

它声明“名称‘userInput’未定义。”但是,当我尝试在初始 while 循环之外打印时,它工作正常。

最佳答案

set userInput = None above while loop

这就是解决方案,谢谢大家!

关于python - 无法定义名称 "userInput",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39500567/

相关文章:

python - 错误: 'Invalid Character in Idnetifier' when trying to solve ODE's in python

python - 在字符串列表中的空格中插入字符

python - 检查所有属性是否作为字典键存在于嵌套元组的一个(且仅有一个)索引中

Python 3 错误 : ValueError: invalid literal for int() with base 10: ''

Python3.9 malloc : can't allocate region error 3

python - 在nodejs中从python激活虚拟环境

Python:更改大写字母

python - 为什么我在递归 DFS 后打印出一个特殊字符?

python - 从另一个文件获取变量 - python

python - 使用 numpy 数组中的索引执行计算和比较