1

在下面的代码中,我遇到了两件事。对于第 40 行(examPoints)和第 47 行(hwkPoints)的函数中的数学运算正在通过第 94 行的列表中的一系列值。它应该把分数加起来,但由于某种原因它不是在通过的每个范围上添加最后一个数字。

例如:对于 myValues[11:13],它传递了数字 75、95 和 97,但它只添加了前 2,而不是最后一个。

我的代码在这里:

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

编辑:对不起,我在让代码在此处正确显示时遇到问题的链接。

4

1 回答 1

4

对于 myValues[11:13],它被传递了数字 75、95 和 97,但它只添加了前 2 而不是最后一个

myValues[11:13]选择两个元素,而不是三个。结束索引 (13) 不包括在该范围内。

阅读解释 Python 的切片符号

于 2013-04-18T05:39:22.870 回答