1

In my Code below I am having issues with 2 things. For one The math in the function on lines 40 (examPoints) and 47 (hwkPoints) is being passed a range of values in a list on line 94. It should be taking the scores and adding them up but for some reason it isn't adding the last number on each range passed.

ex: for myValues[11:13] it is being passed the numbers 75, 95, and 97 but it only adds the first 2 and not the last.

My code is here:

from time import asctime
from time import localtime

def findGrade(myGrade): #argument number in range of 1 - 100 or float myNum?
    if myGrade >= 90:
        letterGrade = "A"
    if myGrade >= 80:
        letterGrade = "B"
    if myGrade >= 70:
        letterGrade = "C"
    if myGrade >= 60:
        letterGrade = "D"
    if myGrade < 60:
        letterGrade = "F"
    return letterGrade

def calculatePointsEarned(hwkGrades, examGrades):
    hwkTotal = float(hwkPoints(hwkGrades))
    #print hwkTotal
    examTotal = float(examPoints(examGrades))
    #print examTotal
    myAverage = (hwkTotal + examTotal) / possiblePoints
    myAverage = myAverage * 100.0
    #myAverage = int(myAverage)
    return myAverage

def totalPoints(hwkGrades, examGrades):
    hwkTotal = int(hwkPoints(hwkGrades))
    examTotal = int(examPoints(examGrades))
    allPoints = str((hwkTotal + examTotal))
    return allPoints


#Create newtimeline using day of week, space, month, space, day, space, year, space time from asctime function
def timeMessage():
    localtime()
    newTimeline = asctime()
    return newTimeline

def examPoints(examValues):
    myGrades = 0
    for grade in examValues:
        myGrades = int(grade) + myGrades
    #print str(myGrades) + " exam\n" 
    return myGrades

def hwkPoints(hwkValues):
    myGrades = 0
    for grade in hwkValues:
        myGrades = int(grade) + myGrades
    #print str(myGrades) + " hwk" 
    return myGrades

def requestMessage (myValues, myTime):
    nameLine = myValues[1] + ", " + myValues[2] + "\t" + myValues[0] + "\t" + myValues[3]
    examScore = "Exam " + "- " + myValues[11] + ", " + myValues[12] + ", " + myValues[13]
    hwkScore = "Homework " + "- " + myValues[4] + ", " + myValues[5] + ", " + myValues[6] + ", " + myValues[7] + ", " + myValues[8]  + ", " + myValues[9] + ", " + myValues[10]
    pointsEarned = "Total points earned " + "- " + totalPoints(myValues[4:10], myValues[11:13])
    myGrade = "Grade:  " + str(theAverage) + " that is a " + findGrade(theAverage)
    message = nameLine + "\n" + examScore + "\n" + hwkScore + "\n" + pointsEarned + "\n" + myGrade + "\n" + myTime
    return message

def fileOutput(studentID, lastName, firstName, dateTime):
    myLine = studentID + " " + lastName + ", " + firstName + " " + dateTime + "\n"
    return myLine


#Create input stream from grades.dat and assign to infile
inFile = open('grades.dat', "r")

#Create output stream to results.dat and assign to outfile
outFile = open('results.dat', 'w+')

myTime = timeMessage()
theAverage = 0
theLettergrade = ""
theMessage = ""
possiblePoints = 550
theRequest = ""

studentID = raw_input("Please input student ID: ")
myLine = "notemptystring"

while myLine != "": #may be wrong Is myline not equal to the empty string?
    myLine = inFile.readline() #may be wrong Read line from infile and assign to myline
    myLine = myLine.strip('\r\n')

    myValues = myLine.split(",") #Separate values in myline and assign to myvalues

    if studentID == myValues[0]:
        #print myValues

        #Call calculatePointsEarned function with a list of homework values and a list of exam values and assign to theaverage
        theAverage = calculatePointsEarned(myValues[4:10], myValues[11:13])
        #print theAverage

        theLettergrade = findGrade(theAverage) #Call findGrade function with theaverage and assign to thelettergrade)
        #print theLettergrade

        #Call fileOutput function with studentid, lastname, firstname, and datetime to get message for writing to output file, and assign to therequest
        theRequest = fileOutput(myValues[0], myValues[1], myValues[2], timeMessage())
        #print theRequest

        theMessage = requestMessage(myValues, timeMessage()) #Call requestMessage with myline and mytime for message to display, and assign to themessage

        #Write theRequest to outfile
        outFile.write(theRequest)

        print theMessage

    else: #is studentid not equal to first element in myValues
        "Do Nothing"


#close infile and outfile
inFile.close()
outFile.close()

Edit: Sorry for the link I was having problems getting the code to look right on here.

MagikCow
  • 863
  • 8
  • 13
  • 3
    Please post the relevant (not all of it) code on this site, not through a link – jamylak Apr 18 '13 at 05:35
  • 1
    The end-point of a slice or index in Python is non-inclusive- you have to give an endpoint that's one past the last value you want. – Marius Apr 18 '13 at 05:36

1 Answers1

4

for myValues[11:13] it is being passed the numbers 75, 95, and 97 but it only adds the first 2 and not the last

myValues[11:13] selects two elements, not three. The end index (13) is not included in the range.

Have a read of Explain Python's slice notation

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012