local variable 'moodsc' referenced before assignment
Question: In the code below I get the following error:

"local variable 'moodsc' referenced before assignment"

I'm new to programming and python. I'm struggling with interpreting other
questions on the similar topic. Any context around this specific code would be
helpful.

    
    
    import re
    import json
    import sys
    
    def moodScore(sent, myTweets):
    
      scores = {} # initialize an empty dictionary
      new_mdsc = {} # intitalize an empty dictionary
      txt = {}
    
      for line in sent:
        term, score  = line.split("\t")  # The file is tab-delimited. "\t" means "tab character"
        scores[term] = int(score)  # Convert the score to an integer.
    
      data = [] # initialize an empty list
    
      for line in myTweets:
        tweet = json.loads(line)
        if "text" in tweet and "lang" in tweet and tweet["lang"] == "en":
          clean = re.compile("\W+")
          clean_txt = clean.sub(" ", tweet["text"]).strip()
          line = clean_txt.lower().split()  
    
      moodsc = 0
      pos = 0
      neg = 0
      count = 1
    
      for word in range(0, len(line)):
        if line[word] in scores:
           txt[word] = int(scores[line[word]])
        else: 
           txt[word] = int(0)
        moodsc += txt[word]
        print txt 
    
        if any(v > 0 for v in txt.values()): 
          pos = 1 
        if any(v < 0 for v in txt.values()): 
          neg = 1 
    
        for word in range(0, len(line)): # score each word in line
          if line[word] not in scores:
            if str(line[word]) in new_mdsc.keys():
              moodsc2 = new_mdsc[str(line[word])][0] + moodsc
              pos2 = new_mdsc[str(line[word])][1] + pos
              neg2 = new_mdsc[str(line[word])][2] + neg
              count2 = new_mdsc[str(line[word])][3] + count
              new_mdsc[str(line[word])] = [moodsc2, pos2, neg2, count2]
            else:
              new_mdsc[str(line[word])] = [moodsc, pos, neg, count] 
    
    def new_dict():
      for val in new_mdsc.values():
        comp = val[0] / val[3]
        val.append(comp)
    
      for key, val in new_mdsc.items():
        print (key, val[4])
    
    def main():
        sent_file = open(sys.argv[1])
        tweet_file = open(sys.argv[2])
        moodScore(sent_file, tweet_file)
    #    new_dict()
    
    if __name__ == '__main__':
        main()
    


Answer: Ok @joshp, I think you need to globalise some variables, because the error is
'moodsc referenced before assignment', I think the code only gets as far as
`moodsc += txt[word]` but you may also have trouble with `pos` and `neg`.

Try `global moodsc` and `pos` etc. before you define `moodsc` and `pos` etc.
If this doesn't work try `global moodsc` before `moodsc += txt[word]` and so
forth, you may need to use global in both places for it to work, I often find
that this is needed in my code, to globalise it at definition and wherever
else you use it (at the start of each function and statement where it is
used).

