Home > Back-end >  Numpy: change score to 4 GPA score
Numpy: change score to 4 GPA score

Time:11-13

I would like to write a short code (maybe with the use of numpy or any other mathematical package) for calculating a score I recive in python according to the 4 GPA formola like in the link

Means that, If Im receing this list of scores:

[95,100,80,42]

Ill receive this result:

[4.0,4.0,2.7,0.0]

CodePudding user response:

This solves your problem:

my_list = [95,100,80,42]

#percent grade to 4.0 scale

def convert_to_4_scale(list):
    for i in range(len(list)):
        if list[i] >= 90:
            list[i] = 4.0
        elif list[i] >= 80:
            list[i] = 3.0
        elif list[i] >= 70:
            list[i] = 2.0
        else:
            list[i] = 1.0
    return list

print(convert_to_4_scale(my_list))

Output --> [4.0, 4.0, 3.0, 1.0]


CodePudding user response:

list1 = [95, 100, 80, 42]


def GPA(inp):
    for i in list1:
        if i >= 90:
            print(4.0)
        elif i >= 80:
            print(3.0)
        elif i >= 70:
            print(2.0)
        else:
            print(1.0)


GPA(list1)

output

4.0
4.0
3.0
1.0

Here I have used a list and created a Function where it will check the condition and give output

CodePudding user response:

import numpy as np

l=np.array([95,100,80,42])

result=np.empty(shape=l.shape)

result[l>=90]=float(4)

result[(l<90) & (l>=50)]=float(2.7)

result[l<50]=float(0)

print(result)

  • Related