1

以下の私のコードでは、2 つの問題があります。40 行目 (examPoints) と 47 行目 (hwkPoints) の関数の数学は、94 行目のリストの値の範囲を渡されています。スコアを取得して加算する必要がありますが、何らかの理由でそうではありません。渡された各範囲の最後の番号を追加します。

例: myValues[11:13] の場合、数値 75、95、および 97 が渡されますが、最後の 2 ではなく最初の 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 ではなく最初の 2 のみが追加されます。

myValues[11:13]3 つではなく 2 つの要素を選択します。終了インデックス (13) は範囲に含まれません。

Explain Python のスライス表記を読んでください。

于 2013-04-18T05:39:22.870 に答える