Contents
- average2.py
- avgData.py
- file_search.py
- file_write.py
- writeSumReport2.py
- writeSumReport.py
average2.py 1/6
[top][prev][next]
# Program to find the average of two numbers
# by Sara Sprenkle
def average2(num1, num2):
"""
returns the average of two numbers
Parameters: two numbers
"""
average = (num1 + num2)/2
return average
print("This program will find the average of two numbers.")
print()
num1 = eval(input("Enter the first number: " ))
num2 = eval(input("Enter the second number: "))
# call the average function
average = average2(num1, num2)
print("The average of", num1, "and", num2, "is", average)
avgData.py 2/6
[top][prev][next]
# Computes the average high temperature from a file that contains the daily
# high temperatures for last year at one location.
# CSCI 111, 02.17.2012
DATAFILE="data/alaska.dat"
tempFile = open(DATAFILE, "r")
total = 0
numberOfLines = 0
for line in tempFile:
# add every line together
total += float(line)
numberOfLines += 1
tempFile.close()
average = total / numberOfLines
print("The average high temperature is %.2f" % average)
file_search.py 3/6
[top][prev][next]
# Given a file and a term to search for,
# find which lines the term is on and the
# total number of lines that contained that term
# CSCI 111, 2/17/2012
FILENAME="data/years.dat"
print("This program will find out how many lines the given search term")
print("is on and show those lines")
print()
print("Looking at file", FILENAME)
searchFor = input("What term do you want to search for? ")
# keeps track of the line number in the file
lineNumber = 0
# keep track of the lines where the term is found
foundIn = ""
# keeps track of the number of lines the term is found
linesFound = 0
# open the file in read mode
dataFile = open(FILENAME, "r")
# Read through each line in the file
for line in dataFile:
lineNumber += 1
if searchFor in line:
linesFound += 1
foundIn += ("Line %4d: " % lineNumber) + line
# always close the file when done reading/writing from file
dataFile.close()
# display the results
print("\"", searchFor , "\" occurs on", linesFound)
print("Found on:")
print(foundIn)
# Alternative solution:
# Read contents from the file and split on the new lines to break
# into lines, e.g.,
#contents = dataFile.read()
#lines = contents.split("\n")
#for line in lines:
file_write.py 4/6
[top][prev][next]
# Writes content from a user to a file
# by Sara Sprenkle
PROMPT = "Enter the next line in the file: "
outfilename = input("What is the name of your output file? ")
numLines = eval(input("How many lines do you want to write? "))
# create a new file object, in "write" mode
dataFile = open(outfilename, "w")
for x in range(numLines):
userinput = input(PROMPT)
# write the user's input to the file
dataFile.write(userinput)
# write a newline after each input from the user
dataFile.write("\n")
# close the file with the method "close"
dataFile.close()
writeSumReport2.py 5/6
[top][prev][next]
# Given a file containing students names and their years (freshman, sophomore,
# junior, or senior) for this class, creates a report (in a file) that
# says the year and how many students from that year are in this class, on the
# same line.
#
# CSCI 111, 02.27.2012
FILENAME="data/years.dat"
REPORTNAME="data/report.dat"
def numLinesOccursOn(searchTerm, filename):
"""
returns an integer representing the count of the number of lines the
searchTerm occurs in the file
Parameters:
searchTerm - a string representing what to search for
filename - the name of the file to search
"""
# open the file in read mode
dataFile = open(filename, "r")
linesFound = 0
# Read through each line in the file
for line in dataFile:
# figure out if the search term is in the line
if searchTerm in line:
linesFound += 1
dataFile.close()
return linesFound
searchTerms = ["FY", "SO", "JR", "SR" ]
reportFile = open(REPORTNAME, "w")
for searchTerm in searchTerms:
numLines = numLinesOccursOn(searchTerm, FILENAME)
reportFile.write("%2s %3d\n" % (searchTerm, numLines))
reportFile.close()
writeSumReport.py 6/6
[top][prev][next]
# Given a file containing students names and their years (freshman, sophomore,
# junior, or senior) for this class, creates a report (in a file) that
# says the year and how many students from that year are in this class, on the
# same line.
#
# CSCI 111, 02.27.2012
FILENAME="data/years.dat"
REPORTNAME="data/report.dat"
yearsFile = open(FILENAME, "r")
searchTerms = ["FY", "SO", "JR", "SR" ]
# represents the number of times each of the search terms occurs in the file
searchCounts = [0]*4 # creates a list, containing four elements, all 0s
# count the number of times the terms occur in the file
for line in yearsFile:
for pos in range(len(searchTerms)):
if searchTerms[pos] in line:
searchCounts[pos]+=1
yearsFile.close()
#############################################################
# write the results into the report file
reportFile = open(REPORTNAME, "w")
for pos in range(len(searchCounts)):
term = searchTerms[pos]
count = searchCounts[pos]
reportFile.write("%2s %3d\n" % (term, count))
reportFile.close()
Generated by GNU enscript 1.6.4.