What have you done/learned
- ทำการออกแบบโปรแกรมคำนวณเกรดเฉลี่ยโดยใช้ภาษา Python ในการอ่านไฟล์ CSV
( Comma-Separated Value ) - ศึกษาเกี่ยวกับการองค์ประกอบของ Database และส่วนประกอบต่างๆของตาราง
- ทำการสร้างตารางใหม่เพื่อใช้สำหรับการคำนวณค่า GPA
ภาพตารางบันทึก grade
Problem/Solution
- ค่าของเกรดมีการปัดขึ้นหากให้ %.2f จึงจำเป็นต้องใช้ library ตัวอื่นมาช่วย คือ Decimal โดยใช้คำสั่ง
โค้ดที่ใช้ในการแก้ปัญหาการปัดเลขขึ้น โดยแบ่งคำสั่งออกเป็น Decimal(ค่าที่ใช้.quantize(Decimal('.01) > ตำแหน่งของทศนิยมที่ต้องการ, rounding=ROUND_DOWN)) > ปัดเลขทศนิยมลง
- การบวกค่าทั้งหมดในตารางหากทำการตัดโดยใช้ช่องว่างระหว่างตารางจะไม่สามารถคิดของบรรทัดสุดท้ายได้ จึงได้เพิ่มฟังค์ชันการนับจำนวนของแถวเพื่อใช้เทียบกับตำแหน่งของแถวปัจจุบัน
- ในส่วนของวิธีการคำนวณได้เลือกใช้การแปลงค่าจากเกรดให้เป็นตัวเลขที่ใช้ในการคำนวณแทนการใช้ระดับคะแนนโดยตรงเพื่อทดสอบการออกแบบเงื่อนไขสำหรับใช้คำนวณ
ภาพตัวอย่างเงื่อนไขที่ใช้ในการคำนวณ
เนื่องจากการใช้ if else แบบนี้ทำให้ต้องทำการเขียนคำสั่งเดิมซ้ำๆซึ่งไม่เหมาะสมในการทำงาน จึงได้มีการปรับปรุงและแก้ไขเป็นแบบการดึงค่าที่ตรงกับ array ที่กำหนดมาใช้
Related Info/link/reference
- การใช้ Library ของ Decimal : https://docs.python.org/2/library/decimal.html
- การออกแบบฟังค์ชันการนับจำนวนตาราง : https://stackoverflow.com/questions/970983/have-csv-reader-tell-when-it-is-on-the-last-line
- ตัวอย่างโค้ด
import csv
from decimal import Decimal, ROUND_DOWN
filename = 'GPA_Calculate.csv'
grade = 0
total_grade = 0
credit = 0
total_credit = 0
gpax = 0
grade_list = ['A', 'B+', 'B', 'C+', 'C', 'D+', 'D', 'F']
score_list = [4, 3.5, 3, 2.5, 2, 1.5, 1, 0]
def gpa_calculate(subject):
for i in range(len(grade_list)):
if subject[3] == grade_list[i]:
return Decimal(score_list[i] * float(subject[2]))
with open(filename) as in_file:
last_line = sum(1 for _ in in_file)
reader = csv.reader(open(filename))
for row in reader:
if row[3] == '':
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
grade = 0
credit = 0
elif last_line == reader.line_num:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
else:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])
from decimal import Decimal, ROUND_DOWN
filename = 'GPA_Calculate.csv'
grade = 0
total_grade = 0
credit = 0
total_credit = 0
gpax = 0
grade_list = ['A', 'B+', 'B', 'C+', 'C', 'D+', 'D', 'F']
score_list = [4, 3.5, 3, 2.5, 2, 1.5, 1, 0]
def gpa_calculate(subject):
for i in range(len(grade_list)):
if subject[3] == grade_list[i]:
return Decimal(score_list[i] * float(subject[2]))
with open(filename) as in_file:
last_line = sum(1 for _ in in_file)
reader = csv.reader(open(filename))
for row in reader:
if row[3] == '':
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
grade = 0
credit = 0
elif last_line == reader.line_num:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])
gpa = Decimal((grade / credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
gpax = Decimal((total_grade / total_credit).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(grade, total_grade, credit, total_credit, gpa, gpax)
else:
grade += gpa_calculate(row)
total_grade += gpa_calculate(row)
credit += Decimal(row[2])
total_credit += Decimal(row[2])
ไม่มีความคิดเห็น:
แสดงความคิดเห็น