'''A program that reads students' scores from a text file to calculate and determine their letter grades. Uses separate functions to read data, process data, determine letter grade, write students' records, and a main to call the functions and print. Completed by: Caroline McCraw & Zach Stella ''' def readData (fileName): #function to read students' data line by line from file inFile = open(fileName, 'r') #opens student data file lineByLine = inFile.readlines() #reads file by line return(lineByLine) #isolates each student's data as its own line def processData (): studentLine = lineByLine for i in studentLine: #for loop to calculate each student's average score studentData.split() #splits string data into items score1 = float(studentData[2]) #**can simplify this later score2 = float(studentData[3]) score3 = float(studentData[4]) score = (score1 + score2 + score3)/3 #averages student's final score return score #invoke letterGrade function #strip end-of-line and concatenate average an letterGrade to student # add each student data into a new (all) students string, concatenate \n to end of each line #return student records def letterGrade(score): #function to determine letter grade based on student's averaged score #PARAMETER avg score if score >= 90: letter = 'A' elif score >= 80: letter = 'B' elif score >= 70: letter = 'C' elif score >= 60: letter = 'D' else: letter = 'F' return letter def writeScores(): #writes student grades in separate file #PARAMETER student data as string and filename outFile = open('mcCrawStellaStudentGrades.txt', 'w') outFile.write(studentOutput) def main(): readData("studentData.txt") #print file content # invoke process grade to update students' data w/ grades #print student records #invoke writeScores main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question