1

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])
  • You need to fix your formatting. – Guy Feb 14 '18 at 12:06
  • I'm sorry if the code isn't very clear, usually the code I work on is my eyes only, what alterations to the formatting would I make? – James Henry Feb 14 '18 at 12:20
  • If you use that piece of code, it doesn't raises Error. What's the code triggering the error? In your code, just make sure to call the function `readinSuspects` before trying to acces the `Suspect_ID` variable. – jtagle Feb 14 '18 at 12:21
  • I've edited the original post, is this what you mean by ensuring I call the function, I am a student still so if I get anything wrong I can only apologise – James Henry Feb 14 '18 at 12:39

1 Answers1

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

stefan.stt
  • 2,357
  • 5
  • 24
  • 47
  • Ahh so the local variables still needed somewhere to go to be stored globally, I see what you mean, thank you very much for the help, it has been a life saver. – James Henry Feb 14 '18 at 13:11