Python: for key in gasCount: NameError: name 'gasCount' is not defined -
import string def getfile(): filename = input('filename: ') #the file name should .csv file = open(filename, 'r') firstline = true line = file.readline() if line == none or line == '': return none if firstline: # not want read field names line = file.readline() # there more read firstline = false # skip them. code assuems return file #count number of (t's) in field [correct] def calct(correct): global tcount found = false ch in correct:#look @ each character in turn if ch in 'tt': found = true if found: tcount +=1 #how many times least common string appear in field [gas] def gas_count(gas): global gascount if gas in gascount: gascount[gas] += 1 else: gascount[gas] = 1 smallest = 100000 key in gascount: if gascount[key] < smallest: smallest = gascount[key] answers = [] key in gascount: if gascount[key] == smallest: answers.append(key) #find sum of values in field [quant] less (408) def sum_quant(quant): global qsum if quant < 408: qsum += quant code="0" #how many values in 'code' field not match 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 average value of numbers in field [age] in range (30) , (107) inclusive def average_age(age): global tage global agecount if age >= 30 , age <=107: tage += age agecount += 1 #find sum of numbers in field [length] between (2.482) , (6.428) inclusive def sum_length(leng): global lensum if leng >= 2.482 , leng <= 6.428: lensum += leng #count lines gas's have value (nitrogen) *or* quant less 318 def calcgas(gas, quant): global clines if gas == 'nitrogen' or quant < 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): line in data: processline(line) data.close() def displayresults(): #count number of (t's) in field [correct] print('the number of (t) in field [correct]: %d'%(tcount)) print('-' *10) print(gascount) print('-' *10) print(ccount) #find sum of values in field [quant] less (408) print('the sum of values in field [quant] less (408): %d'%(qsum)) print('-' *10) #how many values in 'code' field not match format 9999(x9+)9? #print('the values in code field not match format 9999(x9+)9: %d'%() print('-' *10) #what average value of numbers in field [age] in range (30) , (107) inclusive print((tage/agecount)) print('-' *10) #find sum of numbers in field [length] between (2.482) , (6.428) inclusive print('the sum of numbers in field [length] between (2.482) , (6.428): %6.3f'%(lensum)) print('-' *10) #count lines gas's have value (nitrogen) *or* quant less 318 print('the lines gas have value (nitrogen) *or* quant less 318: %d' %(clines)) tcount = 0 gascount = {} qsum = 0 ccount = 0 agecount = 0 tage = 0 lensum = 0 clines = 0 myfile = getfile() processfile(myfile) displayresults()
getting error:
traceback (most recent call last): file "c:\users\ultra\downloads\3215402\3215402\3215402 stage 1 function.py", line 63, in <module>
editing tobias_k:
def gas_count(gas): global gascount if gas in gascount: gascount[gas] += 1 else: gascount[gas] = 1 smallest = 100000 key in gascount: if gascount[key] < smallest: smallest = gascount[key] answers = [] key in gascount: if gascount[key] == smallest: answers.append(key)
gives error
traceback (most recent call last): file "c:\users\ultra\downloads\3215402\3215402\3215402 stage 1 function.py", line 57, in <module> key in gascount: nameerror: name 'gascount' not defined
Comments
Post a Comment