2013-04-18 59 views
1

在我的代碼中,我遇到了兩件事情。對於第40行(examPoints)和第47行(hwkPoints)函數中的數學,將在94行的列表中傳遞一系列值。它應該將分數加起來,但由於某種原因,它不是添加傳遞的每個範圍的最後一個數字。Python從列表中傳遞的值不能正確添加

例如:對於myValues [11:13]它正在通過數字75,95和97,但它只增加了前兩個,而不是最後一個。

我的代碼是在這裏:

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() 

編輯:對不起,我有問題越來越代碼看的權利在這裏的鏈接。

+3

讀請張貼有關(不是全部)代碼在這個網站,而不是通過一個鏈接 – jamylak 2013-04-18 05:35:38

+1

Python中的切片或指數的終點是無inclusive-你有給出一個超過您想要的最後一個值的端點。 – Marius 2013-04-18 05:36:53

回答

4

爲myValues [11:13]它被傳遞的數字75,95,和97,但只將第一2和不是最後

myValues[11:13]選擇兩個元件,而不是三個。結束索引(13)未包含在範圍內。

Explain Python's slice notation