I do not even know where to begin! Any help would be great! Write a Python progr
ID: 3631168 • Letter: I
Question
I do not even know where to begin! Any help would be great!Write a Python program to read in a file of student letter grades (in a file named grades.txt), and display a count of the grades. The count of the grades are kept in a list. The grades are uppercase letters A-E. Some grades may not yet be assigned and be another uppercase letter. If a grade is not A-E display (for example):
Grade N on line 2 is not A-E
The file will contain only uppercase letters.
An example program execution using the file grades.txt:
C A B B A
D A C N
B C X C D E
C B A C C
Grade N on line 2 is not A-E
Grade X on line 3 is not A-E
[4, 4, 7, 2, 1]
The [4, 4, 7, 2, 1] is a display of the list of grades. Just print the list variable.
What you must use for this program:
-open/close file commands
-for loop to read lines from the file
-a list to keep the counts of the grades
-split function to split a line read from the file into individual grades
-for loop to read individual grades from the list created by the split command
-if-elif-else decisions to determine the grade
Explanation / Answer
import fileinput
fd = open( "c:\test.txt" )
linecount = 0
gradesList = [0, 0, 0, 0, 0]
for line in fd.readlines():
linecount = linecount + 1
line = str.replace(line," ","")
grades = list()
grades = str.split(line," ")
for grade in grades:
#print grade
if (grade=="A"):
gradesList[0] = gradesList[0] + 1
elif (grade=="B"):
gradesList[1] = gradesList[1] + 1
elif (grade=="C"):
gradesList[2] = gradesList[2] + 1
elif (grade=="D"):
gradesList[3] = gradesList[3] + 1
elif (grade=="E"):
gradesList[4] = gradesList[4] + 1
else:
print "Grade "+ grade +" on line "+ str(linecount) +" is not A-E"
#print line
fd.close()
outstring = "["
for item in gradesList:
outstring = outstring + str(item) + " "
outstring = outstring + "]"
print outstring
raw_input("")
Note: Please maintain the indentation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.