Python 3 : Suspect_ID is not defined

标签 python python-3.x

我目前正在为一门类(class)开展 Salem-esc 镇的项目,但我遇到了一个问题。我一直在尝试使 Suspect_ID 在全局范围内可用,但由于某种原因,它改为说(“名称'Suspect_ID'未定义”),我尝试将其与其余的全局变量一起放在语句之外也无济于事。任何建议都会有帮助,如果需要任何其他信息,请随时询问,我希望您在这个程序上的运气比我目前的好。

def readinSuspects():

    #local variables
    global Suspect_name
    Suspect_name=["","","","","","","","","",""]
    global Suspect_age
    Suspect_age=[0,0,0,0,0,0,0,0,0,0]
    global Suspect_motive
    Suspect_motive=["","","","","","","","","",""]
    global Suspect_ID
    Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
    global IsMurderer
    IsMurderer=[False,False,False,False,False,False,False,False,False,False]
    #subprogram body
    file = open("suspects.txt","r")
    for i in range(0,9):
        Suspect_name[i],Suspect_age[i],Suspect_motive[i], 
    Suspect_ID[i]=file.readline().split(',')
    return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer

编辑:我现在意识到问题可能出在其他地方,因此以下将是完整的程序,它还远未完成,我知道还有许多其他错误等。

import random

#Global variable
Guesses=[0]
Murderer=[0]

#Read In Function
def readinSuspects():
    #local variables
    global Suspect_name
    Suspect_name=["","","","","","","","","",""]
    global Suspect_age
    Suspect_age=[0,0,0,0,0,0,0,0,0,0]
    global Suspect_motive
    Suspect_motive=["","","","","","","","","",""]
    global Suspect_ID
    Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
    global IsMurderer
    IsMurderer=[False,False,False,False,False,False,False,False,False,False]
    #subprogram body
    file = open("suspects.txt","r")
    for i in range(0,9):
        Suspect_name[i],Suspect_age[i],Suspect_motive[i], 
Suspect_ID[i]=file.readline().split(',')
    return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer


#randomly assign murderer
readinSuspects(Suspect_ID)
Murderer = random.randint(0,9)
for i in range(0,9):
    if Murderer == i:
#print Suspect_ID if working
        print(Suspect_ID[i])

最佳答案

首先,您的方法 readinSuspects() 不接受参数,但您使用一个参数 - Suspect_ID 调用它,该参数尚未定义。

我已经修改了您的代码,所以现在它必须可以工作:

import random

#Global variable
Suspect_name = []
Suspect_age = []
Suspect_motive = []
Suspect_ID = []
IsMurderer = []

Guesses=[0]
Murderer=[0]

#Read In Function
def readinSuspects():
    #local variables
    global Suspect_name
    Suspect_name=["","","","","","","","","",""]
    global Suspect_age
    Suspect_age=[0,0,0,0,0,0,0,0,0,0]
    global Suspect_motive
    Suspect_motive=["","","","","","","","","",""]
    global Suspect_ID
    Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
    global IsMurderer
    IsMurderer=[False,False,False,False,False,False,False,False,False,False]
    #subprogram body
    file = open("suspects.txt","r")
    for i in range(0,9):
        Suspect_name[i],Suspect_age[i],Suspect_motive[i], Suspect_ID[i]=file.readline().split(',')
    return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer


#randomly assign murderer
readinSuspects()
Murderer = random.randint(0,9)
for i in range(0,9):
    if Murderer == i:
#print Suspect_ID if working
        print(Suspect_ID[i])

另请阅读有关 if __name__ == '__main__': - 这是一个很好的 Python 实践,但没有它它仍然可以工作。在这里您可以阅读如何在 python 中定义全局变量 How to define global variables in python SO

你的代码还有很多需要讨论的地方,但我会让你的老师去做;)

关于Python 3 : Suspect_ID is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48786748/

相关文章:

string - 使用字典选择不重叠的最大子串总和

python - 向结构化numpy数组添加字段(二)

python - 轮廓标签未显示

python - 如何对列表中的列表执行数学函数

python-3.x - Django : Error trying to save form

python - 如何确保 filehandle.write() 不会因 str/bytes 转换问题而失败?

php - 在 Python 中使用 flask 进行 Hmac 验证(在 PHP 和 RUBY 中有引用)

python - matplotlib:一张图上的多个图

python - 将列名转换为python中的变量

python - 如何以正确的方式在 Python 中连接到 MySQL 数据库?