making a pie chart question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ri0o
    New Member
    • Nov 2009
    • 3

    making a pie chart question

    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.ucal gary.ca/~zongpeng/CPSC231/assignments/A3/a3.pdf

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

    i was able to extract the 6th column with this code
    Code:
    infile = open('grades1.txt','r')
    all_lines = infile.readlines()
    all_lines = all_lines[1:]
    all_lines = all_lines[:-2]
    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

    Code:
    A+ =[10]
    A = [9,10)
    A- =[8,9)
    B+ =[7,8)
    B =[6,7)
    B- =[5,6)
    C+ =[4,5)
    C =[3,4)
    D+ =[2,3)
    F =[0,1)
    
    
    grades=["ABCDA"]
    i=0
    total=0
    while i<len(grades):
        if grades[i]  =='A':
            total=total+1
            i=i+1
    print total
    any help would be really appreciated
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    f = open("grades.txt")
    sixthCol = [line.split()[5] for line in f.readlines()[1:-1]]
    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:
    Code:
    >>> gradeA1 = sum([float(dd[key]['A1']) for key in dd])
    >>> A1
    1128.0
    >>>
    Here's a hint to create the dictionary for each line in the file:
    Code:
    >>> listA = ['A', 'B', 'C']
    >>> listB = ['12', '13', '14']
    >>> dict(zip(listA, listB))
    {'A': '12', 'C': '14', 'B': '13'}
    >>>
    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:
    Code:
    >>> grade = '7.5'
    >>> gradeDict[int(float(grade))]
    'B+'
    >>>
    Good luck. Post again if you have more questions.
    HTH
    BV

    Comment

    Working...