Connecting Tech Pros Worldwide Help | Site Map

making a pie chart question

Newbie
 
Join Date: Nov 2009
Posts: 8
#1: 3 Weeks Ago
hi, i'm new in python programming
i'm trying to write a simple program that processes a text file containing grades for a class, extracts the desired grades, count the number of grades in each grade segment and genrate a pie chart for grades,(A1) .
http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A3/a3.pdf

txt file:http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A3/grades1.txt

i was able to extract the 6th column with this code
Expand|Select|Wrap|Line Numbers
  1. infile = open('grades1.txt','r')
  2. all_lines = infile.readlines()
  3. all_lines = all_lines[1:]
  4. all_lines = all_lines[:-2]
  5.  
i need help in assigning a grade to the results
and that's what i got so far but it doesn't seem to be working

Expand|Select|Wrap|Line Numbers
  1. A+ =[10]
  2. A = [9,10)
  3. A- =[8,9)
  4. B+ =[7,8)
  5. B =[6,7)
  6. B- =[5,6)
  7. C+ =[4,5)
  8. C =[3,4)
  9. D+ =[2,3)
  10. F =[0,1)
  11.  
  12.  
  13. grades=["ABCDA"]
  14. i=0
  15. total=0
  16. while i<len(grades):
  17.     if grades[i]  =='A':
  18.         total=total+1
  19.         i=i+1
  20. print total
any help would be really appreciated
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: 2 Weeks Ago

re: making a pie chart question


You do not have the sixth column with your code. You have a list of all the lines in the file minus the first and last two lines. The sixth column would be saved with this code:
Expand|Select|Wrap|Line Numbers
  1. f = open("grades.txt")
  2. sixthCol = [line.split()[5] for line in f.readlines()[1:-1]]
  3. f.close()
You should store the data in a form that can be accessed for information. I would suggest a dictionary of dictionaries, but you can tabulate the information in a list of lists or tuples, whatever you are comfortable with. Here's some psuedocode:

Open file, create a file object
Read first line, save as column labels (strip and split the line)
Initialize a dictionary
Iterate on the file object (for line in fObj:)
....Assign dictionary key ID (line[0]) to a dictionary with column labels
........as keys and items (line[1:]) as values.
Close file object.


This can be accomplished with just a few lines of code. A column of grades can be summed like this:
Expand|Select|Wrap|Line Numbers
  1. >>> gradeA1 = sum([float(dd[key]['A1']) for key in dd])
  2. >>> A1
  3. 1128.0
  4. >>> 
Here's a hint to create the dictionary for each line in the file:
Expand|Select|Wrap|Line Numbers
  1. >>> listA = ['A', 'B', 'C']
  2. >>> listB = ['12', '13', '14']
  3. >>> dict(zip(listA, listB))
  4. {'A': '12', 'C': '14', 'B': '13'}
  5. >>> 
You need to find a way to convert a number grade into a letter grade. Here's where a dictionary would work well again. Assume a '9.5' is the same as a '9', and we can take the integer. Assign the intergers 0-10 to the corresponding letter grade. Then we can do this:
Expand|Select|Wrap|Line Numbers
  1. >>> grade = '7.5'
  2. >>> gradeDict[int(float(grade))]
  3. 'B+'
  4. >>> 
Good luck. Post again if you have more questions.
HTH
BV
Reply


Similar Python bytes