"""This is static program if you want to use with other csv file, you need to change some variable""" import csv #import to enable read csv file semester_range = [[3,10],[15,22],[27,33],[38,41],[46,50],[55,59],[64,66]] #range of subject that contain in each semester count_row = 0 count_semester = 0 subject = list() #correct info of each subject Ex. Unit, grade GPA=0 #GPA=(Sum_unitxgrade)%Sum_Unit unit_sum = 0 unitxgrade_sum = 0 with open('GPA.csv',encoding="utf8") as csvfile: #open file spamreader = csv.reader(csvfile) #set opened file as variable name spamreader for row in spamreader: #read the file in each row count_row=count_row+1 #count row if (count_semester<=len(semester_range)-1): #check semester if (count_row>=semester_range[count_semester][0] and count_row<=semester_range[count_semester][1]): #row is in semester range subject.append([row[1],row[2]]) #correct in subject list if (count_row==semester_range[count_semester][1]): #if this isn't last semester count_semester=count_semester+1 #then count semester +1 for i in range(len(subject)): #loop for calculate GPA subject[i].append(float(subject[i][0])*float(subject[i][1])) #calculate unit*grade for unit_sum = unit_sum + float(subject[i][0]) #calculate sum of unit unitxgrade_sum = unitxgrade_sum + float(subject[i][2]) #calculate sum of unit*grade GPA=unitxgrade_sum/unit_sum #calculate GPA print ("GPA = "+str(GPA))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question