Python 3: Suspect_ID is not defined

James Henry Source

I'm currently working on a town of salem-esc project for a class but I've ran into an issue. I have been trying to make it so that Suspect_ID is available globally but for some reason it is instead saying ("name 'Suspect_ID' is not defined", I'm tried making it outside of the statement with the rest of my global variables to no avail as well. Any suggestions would be helpful, and if any other information is needed feel free to ask, I hope you have better luck with this program than I am currently having.

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

edit: I'm now realising the issue may lie elsewhere so following is gonna be the program in it's entirety, it's far from finished and I am aware there are many other bugs etc.

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])
pythonpython-3.x

Answers

answered 6 months ago stefan.stt #1

First of all your method readinSuspects() does not take parameters, but you invoke it with one argument - Suspect_ID which is not defined yet.

I have reworked your code so now it must work:

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

Also read about if __name__ == '__main__': - it is a good python practice, but without it it still works. And here you can read how to define global vars in python How to define global variables in python SO

Your code has much more to be discussed, but I will leave your teacher to do it ;)

comments powered by Disqus