title: Python: for key in gasCount: NameError: name 'gasCount' is not defined
body: <pre><code>import string
  
  def getFile():
      filename = input('Filename: ') #the file name should be .csv
      file = open(filename, 'r')
      firstline = True
  
      Line = file.readline()
      if Line == None or Line == '':
          return None
  
      if firstline: # I do not want to read the field names
          Line = file.readline() # there is more to read
          firstline = False # so I skip them. the code assuems 
  
      return file
  
  #Count the number of (T's) in the field [correct]
  def calcT(correct):
      global tCount
      found = False
      for ch in correct:#look at each character in turn
          if ch in 'tT':
              found = True
  
      if found:
          tCount +=1
  
  #How many times does the least common string appear in the field [gas]
  
  def gas_Count(gas):
      global gasCount
      if gas in gasCount:
          gasCount[gas] += 1
      else:
          gasCount[gas] = 1
  
  smallest = 100000
  for key in gasCount:
      if gasCount[key] &lt; smallest:
          smallest = gasCount[key]
  
  answers = []
  for key in gasCount:
      if gasCount[key] == smallest:
          answers.append(key)
  
  
  #Find the sum of the values in the field [quant] less than (408)
  def sum_quant(quant):
      global qsum
      if quant &lt; 408:
          qsum += quant
  code="0"        
  #How many values in the 'code' field do not match the format 9999(x9+)9?
  def checkString(astring):
      if len(code)  != 10:
          return False
      if not astring[0] in string.digits:
          return False
      if not astring[1] in string.digits:
          return False
      if not astring[2] in string.digits:
          return False
      if not astring[3] in string.digits:
          return False
      if not astring[4]=='(':
          return False
      if not astring[5] in string.ascii_lowercase:
          return False
      if not astring[6] in string.digits:
          return False
      if not astring[7]=='+':
          return False
      if not astring[8]==')':
          return False
      if not astring[9] in string.digits:
          return False
      return True
  
  if checkString(code):
      global cCount
      cCount += 1
  
  
  #What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive 
  def average_age(age):
      global tAge
      global ageCount
      if age &gt;= 30 and age &lt;=107:
          tAge += age
      ageCount += 1
  
  #Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive 
  def sum_Length(leng):
      global lensum
      if leng &gt;= 2.482 and leng &lt;= 6.428:
          lensum += leng
  
  #count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
  def calcGas(gas, quant):
      global clines
      if gas == 'Nitrogen' or quant &lt; 318:
          clines += 1
  
  def processLine(Line):
      Line = Line.strip()
      fields = Line.split(',')
  
      correct = fields[0]
      gas = fields[1]
      quant = int(fields[2])
      code = fields[3]
      age = int(fields[4])
      leng = float(fields[5])
      calcT(correct)
      sum_Length(leng)
      calcGas(gas, quant)
      average_age(age)
      sum_quant(quant)
      gas_Count(gas)
      checkString(astring)
  
  def processFile(data):
  
      for line in data:
          processLine(line)
  
      data.close()
  
  def displayResults():
      #Count the number of (T's) in the field [correct]
      print('The number of (T) in the field [correct]: %d'%(tCount))
      print('-' *10)  
      print(gasCount)
      print('-' *10)
      print(cCount)
      #Find the sum of the values in the field [quant] less than (408)
      print('The sum of the values in the field [quant] less than (408): %d'%(qsum))
      print('-' *10)
      #How many values in the 'code' field do not match the format 9999(x9+)9?
      #print('The values in the code field do not match the format 9999(x9+)9: %d'%()
      print('-' *10)
      #What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive
      print((tAge/ageCount))
      print('-' *10)
      #Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive 
      print('The sum of the numbers in field [length] between (2.482) and (6.428): %6.3f'%(lensum))
      print('-' *10)
      #count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
      print('The lines where gas have the value (Nitrogen) *or* quant is less than 318: %d' %(clines))
  
  
  tCount = 0
  gasCount = {}
  qsum = 0
  cCount = 0
  ageCount = 0
  tAge = 0
  lensum = 0
  clines = 0
  myfile = getFile()
  processFile(myfile)
  displayResults()
  </code></pre>
  
  <p>getting error: </p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Ultra\Downloads\3215402\3215402\3215402 Stage 1 Function.py", line 63, in &lt;module&gt;
  </code></pre>
  
  <p>editing to this for tobias_k:</p>
  
  <pre><code>def gas_Count(gas):
      global gasCount
      if gas in gasCount:
          gasCount[gas] += 1
      else:
          gasCount[gas] = 1
      smallest = 100000  
  for key in gasCount:  
      if gasCount[key] &lt; smallest:    
           smallest = gasCount[key]  
  answers = []  
  for key in gasCount:  
      if gasCount[key] == smallest:  
              answers.append(key)  
  </code></pre>
  
  <p>gives this error </p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Users\Ultra\Downloads\3215402\3215402\3215402 Stage 1 Function.py", line 57, in &lt;module&gt;
      for key in gasCount:
  NameError: name 'gasCount' is not defined
  </code></pre>