Python passed values from a list not adding correctly -
in code below having issues 2 things. 1 math in function on lines 40 (exampoints) , 47 (hwkpoints) being passed range of values in list on line 94. should taking scores , adding them reason isn't adding last number on each range passed.
ex: myvalues[11:13] being passed numbers 75, 95, , 97 adds first 2 , not last.
my code here:
from time import asctime 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 asctime function def timemessage(): localtime() newtimeline = asctime() return newtimeline def exampoints(examvalues): mygrades = 0 grade in examvalues: mygrades = int(grade) + mygrades #print str(mygrades) + " exam\n" return mygrades def hwkpoints(hwkvalues): mygrades = 0 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) + " " + 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 grades.dat , assign infile infile = open('grades.dat', "r") #create output stream results.dat , assign 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 wrong myline not equal empty string? myline = infile.readline() #may wrong read line infile , assign myline myline = myline.strip('\r\n') myvalues = myline.split(",") #separate values in myline , assign myvalues if studentid == myvalues[0]: #print myvalues #call calculatepointsearned function list of homework values , list of exam values , assign theaverage theaverage = calculatepointsearned(myvalues[4:10], myvalues[11:13]) #print theaverage thelettergrade = findgrade(theaverage) #call findgrade function theaverage , assign thelettergrade) #print thelettergrade #call fileoutput function studentid, lastname, firstname, , datetime message writing output file, , assign therequest therequest = fileoutput(myvalues[0], myvalues[1], myvalues[2], timemessage()) #print therequest themessage = requestmessage(myvalues, timemessage()) #call requestmessage myline , mytime message display, , assign themessage #write therequest outfile outfile.write(therequest) print themessage else: #is studentid not equal first element in myvalues "do nothing" #close infile , outfile infile.close() outfile.close()
edit: sorry link having problems getting code right on here.
for myvalues[11:13] being passed numbers 75, 95, , 97 adds first 2 , not last
myvalues[11:13]
selects 2 elements, not three. end index (13) not included in range.
have read of explain python's slice notation
Comments
Post a Comment