Contents
- years_dictionary2.py
- years_dictionary.py
years_dictionary2.py 1/2
[top][prev][next]
# Given a file of the form <firstname> <year>
# creates a mapping between the first names and class
# by CSCI 111
FILENAME="data/roster.dat"
# open the file for reading
yearsFile = open(FILENAME, "r")
classYearToCount = {}
for line in yearsFile:
firstNameAndClass = line.split()
className = firstNameAndClass[1]
if className in classYearToCount:
classYearToCount[className] += 1
else:
classYearToCount[className] = 1
yearsFile.close()
for className in classYearToCount:
print(className, classYearToCount[className])
years_dictionary.py 2/2
[top][prev][next]
# Given a file of the form <firstname> <class>
# creates a mapping between the first names and class
# by CSCI 111
FILENAME="data/roster.dat"
# open the file for reading
yearsFile = open(FILENAME, "r")
firstNameToClass = {}
for line in yearsFile:
firstNameAndClass = line.split()
firstName = firstNameAndClass[0]
className = firstNameAndClass[1]
firstNameToClass[firstName] = className
yearsFile.close()
for firstName in sorted(firstNameToClass):
print(firstName, firstNameToClass[firstName])
print()
print("Which students would you like to learn about?")
print("Press enter to exit.")
whichName = input("For which student would you like to know their class year? ")
while whichName != "":
print( whichName, "is in the", firstNameToClass[whichName], "class")
whichName = input("For which student would you like to know their class year? ")
print("Thank you! Come again!")
Generated by GNU Enscript 1.6.6.