Contents

  1. avgData.py
  2. daysOfWeek.py
  3. writeSumFile.py

avgData.py 1/3

[
top][prev][next]
# Read in a file of temperatures and print out the average
# by CS111, 10.19.2007
# 

inputfile = raw_input("Which file to average? ")

# opens the file in "read" mode
dataFile = file(inputfile, "r")

totalTemp=0
numTemps=0

# reads in the file and prints the content of the file
for line in dataFile:
    # removing trailing and beginning whitespace
    line = line.strip()
    if line != "":
        temp = float(line)
        totalTemp += temp
        numTemps += 1

dataFile.close()

# Compute the average

if numTemps != 0 :
    avgTemp = float(totalTemp)/numTemps

    print "The average temperature is %.2f" % avgTemp
else:
    print "The file contained no temperatures."

daysOfWeek.py 2/3

[
top][prev][next]
# Example illustrating list operations: concatenation and iteration
# by Sara Sprenkle, 10.19.2007

weekDays = ["Mon", "Tue", "Wed", "Thu", "Fri"]
weekendDays = ["Sat", "Sun"]

daysOfWeek = weekDays + weekendDays

print "The Days of the Week:"
for day in daysOfWeek:
    print day


print "\nAGAIN!"
for x in xrange(len(daysOfWeek)):
    print daysOfWeek[x]

writeSumFile.py 3/3

[
top][prev][next]
# Create a file that summarizes how many students from each year are
# in the class.
# Not an efficient solution because reads through the file four times.
# We should revisit this problem later.
# by Sara Sprenkle, 10.17.2007

# Constants representing the different years
FRESHMAN = "FR"
SOPHOMORE = "SO"
JUNIOR = "JR"
SENIOR = "SR"

# Constant representing the special symbols of a comment
COMMENT = "#"

def main() :

    infilename = raw_input("What class file do you want to search? ")
    outfilename = raw_input("What is the name of your summary output file? ")

    outFile = file(outfilename, "w")

    outFile.write("# Summary of number of each year in the class\n")

    fr_count = search_file(infilename, FRESHMAN)
    so_count = search_file(infilename, SOPHOMORE)
    jr_count = search_file(infilename, JUNIOR)
    sr_count = search_file(infilename, SENIOR)

    outFile.write("%s %d\n" % (FRESHMAN, fr_count) )
    outFile.write("%s %d\n" % (SOPHOMORE, so_count) )
    outFile.write("%s %d\n" % (JUNIOR, jr_count) )
    outFile.write("%s %d\n" % (SENIOR, sr_count) )    

    outFile.close()

# input: the line to search, the term to search for,
# a boolean for if we should ignore comments or not
# output: returns True iff the string contains the string searchFor
def search_line(line, searchFor, ignoreComments):
        
    # removes the trailing and beginning whitespace
    line = line.strip()

    if ignoreComments: 
        # only do the find if we know that the comment is in the
        # string
        if COMMENT in line:
            # the position of the comment in the string
            commentPos = line.find(COMMENT)
            # get only what's after the comment
            line = line[:commentPos]

    if searchFor in line:
        # no side effects: don't print 
        # print "I found", searchFor, "in line \""+ line + "\""
        return True
    return False
            
# input: the name of the file to search, the term to search for
# output: returns the number of occurences of the string searchFor in
# the file.
# Note: we can make this function more general by having an Boolean
# input parameter for ignoring comments
def search_file(filename, searchFor):
    
    # opens the file in "read" mode
    dataFile = file(filename, "r")
    
    numOccurences = 0
    
    # reads in the file and prints the content of the file
    for line in dataFile:
        # we can ignore comments or not with the last parameter
        if search_line(line, searchFor, True):
            numOccurences += 1
        
    # close the file with the method "close"
    dataFile.close()

    return numOccurences

main()

Generated by GNU enscript 1.6.4.