I am some issues with my homework which is to create a program that reads the csv files to answer the question, unfortunately my gasp of this isn’t as strong as other. However I have written a majority of my code and the idea is clear but it is clear that my syntax is incorrect. According to my assignment I have to answer the question:

1.Count the number of (T's) in the field [correct]
2.How many times does the least common string appear in the field [gas]
3.Find the sum of the values in the field [quant] less than (408)
4.How many values in the 'code' field do not match the format 9999(x9+)9?
5.What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive
6.Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive
7.count the lines where gas's have the value (Nitrogen) or quant is less than 318

So I wrote my code to the best of my ability. My code is

Stage 2 Function

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 least_string(gas):
    if gas in gas_dict:
        gas_dict[gas] += 1
    else:
        gas_dict[gas] = 1
    for key in gas_dict:
        if gas_dict[key] == 1:
            answers.append(key)



#Find the sum of the values in the field [quant] less than (408)
def sum_quant(quant):
    global qsum
    if quant < 408:
        qsum += quant

#How many values in the 'code' field do not match the format 9999(x9+)9?
def checkString(astring):
    if len(astring)  != 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

#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, ageCount
    if age >= 30 and age <=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 >= 2.482 and leng <= 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 < 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]
    if checkString(code):
        global cCount
        cCount += 1
    age = int(fields[4])
    leng = float(fields[5])
    calcT(correct)
    sum_Length(leng)
    calcGas(gas, quant)
    average_age(age)
    sum_quant(quant)
    least_string(gas)


def processFile(data):

    for line in data:
        processLine(line)

    data.close()

def displayResults():
        print('''
Stage 1 Function
The number of (T) in the field [correct]:%d
%s
The least common string appear in the field [gas]:%s
The sum of the values in the field [quant] less than (408): %d
The values in the code field do not match the format 9999(x9+)9: %d
The average value of numbers in the field[age] in range(30)and(107):%0.2f
The sum of the numbers in field [length] between (2.482) and (6.428):%0.3f
The lines where gas have the value (Nitrogen) *or* quant is less than 318: %d
'''%(tCount,gas_dict,answers,qsum,cCount,((tAge/ageCount)),lensum,clines))

##----------------
## This is the main program
##----------------

tCount = 0
qsum = 0
gas_dict = {}
answers =[]
cCount = 0
ageCount = 0
tAge = 0
lensum = 0
clines = 0
myfile = getFile()
processFile(myfile)
displayResults()

So when I run the code, I enter in the filename (3215402a.csv). This works out fine. The it returns a whole list of gas

The least common string appear in the field [gas]:['Helium', ' Chlorine', 'Helium', ' Chlorine', 'Nitrogen', 'Helium', ' Chlorine', 'Nitrogen', 'Methane', 'Helium', ' Chlorine', 'Nitrogen', 'Oxygen', 'Methane', 'Helium', ' Chlorine', 'Nitrogen', 'Methane', 'Helium', ' Chlorine', 'Nitrogen', 'Methane', 'Helium', ' Chlorine', 'Nitrogen', 'Helium', 'Methane', 'Argon', ' Chlorine', 'Nitrogen', 'Helium', 'Methane', 'Hydrogen', 'Argon', ' Chlorine', 'Nitrogen', 'Xenon', 'Helium', 'Methane', 'Hydrogen', 'Argon', ' Chlorine', 'Xenon', 'Helium', 'Methane', 'Hydrogen', 'Argon', ' Chlorine', 'Xenon', 'Helium', 'Methane', 'Argon', ' Chlorine', 'Xenon', 'Methane', 'Argon', ' Chlorine', 'Xenon', 'Argon', ' Chlorine', 'Xenon', 'CarbonDioxide', 'Argon', ' Chlorine', 'Xenon', 'CarbonDioxide', 'Argon', ' Chlorine', 'Xenon', 'CarbonDioxide', 'Argon', 'Xenon', 'CarbonDioxide', 'Argon', 'Xenon', 'CarbonDioxide', 'Argon']

The code should print out

The least common string appear in the field [gas]: ['CarbonDioxide', 'Argon', 'Xenon']

There was one point in the programming process where everything actually worked

{' Chlorine': 3, 'Nitrogen': 3, 'Xenon': 1, 'Oxygen': 3, 'Helium': 2, 'Methane': 3, 'Hydrogen': 2, 'CarbonDioxide': 1, 'Argon': 1}

What in the world am I doing wrong? I have looked at plenty of topics on Stack Overflow and used different method, but I either get an error or values won’t returned. What can I look at so that I can fix my code?

Thank you so much and I greatly appreciate your help!

I think you just print out all the keys in the dictionary and not those with lowest occurance in your display results

This article has been dead for over six months. Start a new discussion instead.