Contents
- file_handle.py
- yearborn.py
- years_dictionary2.py
- years_dictionary.py
file_handle.py 1/4
[top][prev][next]
# Demonstrate file handling exception
# Sara Sprenkle
import sys
def main():
infileName = input("What file do you want to read? ")
try:
inFile = open(infileName, "r")
# normally, would be doing some sort of processing of the file here.
inFile.close()
except IOError as exc: # exc is the name of the thrown exception
print("Error reading \"" + infileName + "\".")
# could be a variety of different problems, so print out
# the exception
print(exc)
sys.exit(1)
outfileName = input("What file do you want to write? ")
try:
outFile = open(outfileName, "w")
# normally, would be doing some sort of processing of the file here.
outFile.close()
except IOError as exc:
print("Error writing \"" + outfileName + "\".")
print(exc)
main()
yearborn.py 2/4
[top][prev][next]
# Demonstrate validating user input
# Modified from a student's code from lab assignment
import sys
def main():
#Program mission statement
print("This program determines your birth year")
print("given your age and the current year \n")
try:
age = int(input("Enter your age: "))
# note difference if use eval instead of int in above
currentyear = int(input("Enter the current year: "))
except:
print("ERROR: Your input was not in the correct form.")
print("Enter integers for your age and the current year")
sys.exit()
if age < 0 or age > 115:
print("Come on: you have to be a reasonable age.")
elif currentyear < 0:
print("You need to have a positive year.")
else:
#Subtract age from current year
birthyear=currentyear - age
#Display output to the user
print("You were either born in", birthyear, "or", birthyear-1)
main()
years_dictionary2.py 3/4
[top][prev][next]
# Given a file of the form <lastname> <year>
# creates a mapping between the years and the number of students.
# by CSCI 111
FILENAME="data/years.dat"
# open the file for reading
yearsFile = open(FILENAME, "r")
yearsToNumStudents = {}
for line in yearsFile:
# split the line into its components
# we can ignore the trailing \n because split handles that for us
data = line.split()
lastname = data[0] # we're not using the lastname
year = data[-1] # we know the last thing is the year
# if the class year is not in the dictionary,
# create a new mapping
if year not in yearsToNumStudents:
yearsToNumStudents[year] = 1
# otherwise, update the mapping
else:
yearsToNumStudents[year] += 1
"""
if yearsToNumStudents.get(year) is None:
yearsToNumStudents[year] = 1
# otherwise
else:
yearsToNumStudents[year] += 1
yearsToNumStudents[year] = yearsToNumStudents[year] + 1
yearsToNumStudents[year] = yearsToNumStudents.get(year) + 1
"""
yearsFile.close()
print( yearsToNumStudents )
for classYear in sorted(yearsToNumStudents):
print(classYear, yearsToNumStudents[classYear])
years_dictionary.py 4/4
[top][prev][next]
# Given a file of the form <lastname> <class>
# creates a mapping between the last names and class
# by CSCI 111
FILENAME="data/years.dat"
# open the file for reading
yearsFile = open(FILENAME, "r")
lastnameToYear = {}
for line in yearsFile:
# split the line into its components
# we can ignore the trailing \n because split handles that for us
data = line.split()
lastname = data[0:-1]
year = data[-1]
lastnameToYear[lastname] = year
yearsFile.close()
print( lastnameToYear )
for lastname in sorted(lastnameToYear):
print(lastname, lastnameToYear[lastname])
Generated by GNU Enscript 1.6.6.