Python 3:未定义可疑的\u ID

Python 3:未定义可疑的\u ID,python,python-3.x,Python,Python 3.x,我目前正在为一个班级的塞勒姆镇esc项目工作,但我遇到了一个问题。我一直在努力使可疑ID在全球范围内可用,但出于某种原因,它改为:“名称‘可疑ID’未定义”,我尝试在声明之外使用我的其他全局变量也没有用。任何建议都会很有帮助,如果需要任何其他信息,请随时询问,我希望您在这个程序中比我目前拥有的运气更好 def readinSuspects(): #local variables global Suspect_name Suspect_name=["","","","",

我目前正在为一个班级的塞勒姆镇esc项目工作,但我遇到了一个问题。我一直在努力使可疑ID在全球范围内可用,但出于某种原因,它改为:“名称‘可疑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
编辑:我现在意识到问题可能在其他地方,所以下面将是整个程序,它还远未完成,我知道还有很多其他的bug等等

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

首先,您的方法
readinspects()
不接受参数,但您使用一个参数调用它-
Suspect\u 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\uuuuu name\uuuu='\uuuu main\uuuu':
-这是一个很好的python实践,但是没有它它它仍然有效。在这里,您可以阅读如何在python中定义全局变量


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

你需要修改你的格式。如果代码不是很清楚,我很抱歉,通常我处理的代码只是我的眼睛,我会对格式做什么修改?如果你使用那段代码,它不会引发错误。触发错误的代码是什么?在代码中,在尝试加入
可疑\u ID
变量之前,请确保调用函数
readinspect
即可。我编辑了原始文章,这就是您所说的,确保调用该函数,我还是一名学生,所以如果我出了什么问题,我只能道歉,因为局部变量仍然需要在某个地方进行全局存储,我明白你的意思,非常感谢你的帮助,这是一个救命稻草。