Python---GPA(绩点)计算器

代码部分:

def get_rule():
    r = open("rule.txt")
    rules = []
    for line in r.readlines():
        line = line.strip('\n')  
        score, point = line.split('	')
        high, low = score.split('~')
        high = int(high)
        low = int(low)
        point = float(point)
        rule = [high, low, point]
        rules.append(rule)
    return rules


def get_score():
    s = open("score.txt")
    scores = []
    for line in s.readlines():
        line = line.strip('\n')
        score, point = line.split(' ')
        score = int(score)
        point = float(point)
        ans = [score, point]
        scores.append(ans)
    return scores


def calculation(rules, scores):
    all_credit = 0
    gpa = 0
    for i in scores:
        score, credit = i[0], i[1]
        all_credit += i[1]
        for j in rules:
            high, low, p = j[0], j[1], j[2]
            if low <= score <= high:
                gpa += credit * p
    gpa /= all_credit
    print(gpa)


def main():
    rule = get_rule()
    score = get_score()
    calculation(rule, score)


if __name__ == '__main__':
    main()

rule.txt:
100~95 4.3
94~90 4
89~85 3.7
84~82 3.3
81~78 3
77~75 2.7
74~72 2.3
71~68 2
67~65 1.7
64~64 1.5
63~61 1.3
60~60 1
60~0 0

score.txt:(第一列为成绩,第二列为学分)
87 2.3
79 3.5
87 3
91 2.5
92 3

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值