The purpose of this activity is to explore how to read data in from a file and process that data.
from google.colab import drive drive.mount('/drive')Run this cell. Follow the prompts to permit your notebook to access your Google drive files.
class-grades.txt
file to be the path to this file in your Google
drive. (Ask for help if you're not sure what this should be.) Now
run the code. You should see the lines from the file being printed out.
def readGrades(): # Create five empty lists here, # one for each of the sets of grades # (assignment, tutorial, midterm, takehome exam, final exam grade) with open('/drive/My Drive/ColabNotebooks/class-grades.txt', 'r') as f: f.readline() # read the first line (headings) and do nothing with it for line in f: print(line) #line = line.strip('\n') #grades = line.split('\t') # Now do something here with the elements in grades # Do some processing with the different lists of gradesIf you just ran the Code cell and didn't see anything happening, did you remember to call the function? (i.e., Don't forget to add a statement
readGrades()
at the end of the code, or in another Code cell, to call
the function.)
for
loop, comment out the code that prints
the line. Then uncomment the line that strips
off the '\n'
character at the end of each line, and uncomment
the line that separates the data pieces with the '\t'
character, storing the pieces in the list called grades
. Print
out grades
.
string
.)
grades
list to the respective lists of
assignment and exam grades. For example, if assignment
was the
name of one of the lists, we could use the line
assignment.append(float(grades[1]))to add the student's assignment grade to the list of assignment grades. You can do something similar for each of the other grades for this student.
grades
list.
readGrades
function, after you have printed one
of your assignment lists and its size, add
lines to call your average function with each of the 5 different assignment
lists. Print out messages to state the average of each assignment.