-2

I have a set of data which represent the grade or level of a specific value.

scores = [90, 70, 60, 50, 40]

So '90' above means the score is 90 out of 100, '70' is 70 out of 100.
And the first element of the list scores[0] is grade A.
The second element scores[1] is grade B.

So I want to input the score from user like yourMark = int(input('Enter your mark: '))
And i want to return the corresponding grade of the mark.

Like:

Enter your mark: 50
Your grade is D

Enter your mark: 35
You failed

Enter your mark: 74
Your grade is B

Enter your mark: 69
Your grade is C

Enter your mark: 92
Your grade is A

I have no clue how to do that properly.
I used some stupid way like getting each value of the list and assigning it to a variable. Then compare the value with the input value by a bunch of if statement.

However there are more than one scores list in my program. Using the way that I tried is time-consuming and not practical.
And I want to work with list but not assigning new variables. So i wonder what is the better way to do this.

Appreciate if you guys could help :)

My stupid attempt

marks = [90, 70, 60, 50, 40]

yourMark = int(input('Enter your mark: '))


def grade(mark):
    markA = marks[0]
    markB = marks[1]
    markC = marks[2]
    markD = marks[3]
    markE = marks[4]

    if mark >= markA:
        print('Your grade is A')
    elif mark >= markB and mark <= markA:
        print('Your grade is B')
    elif mark >= markC and mark <= markB:
        print('Your grade is C')
    elif mark >= markD and mark <= markC:
        print('Your grade is D')
    elif mark >= markE and mark <= markD:
        print('Your grade is E')
    else:
        print('You failed')


grade(yourMark)
WanDur
  • 1
  • 1

2 Answers2

0

If you don't mind using another library (which is very widespread in Python world), you can use numpy. With that it's quite easy:

import numpy as np

def scores_to_marks(scores, scores_thresholds, marks):
    marks_indices = np.digitize(scores, scores_thresholds)
    return marks[marks_indices]
    
marks = np.array(['F','E','D','C','B','A'])
scores_thresholds = [40, 50, 60, 70, 90]
scores = [0, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(scores_to_marks(scores, scores_thresholds, marks))

# answer
# ['F' 'F' 'F' 'F' 'F' 'E' 'D' 'C' 'B' 'B' 'A' 'A']

The idea is that scores_thresholds is a sorted array that defines so called "bins". Then scores is an array of arbitrary length, that defines numbers that belongs to some bins. numpy.digitize function finds the indices of bins, and using these indices we index the array of marks

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
0

Maybe you can use pandas DataFrame for getting an overview about your different scores lists and use another input field so that you can choose your scores list.

You can do it this way:

import pandas as pd

#input fields
yourMark = int(input('Enter your mark: '))
yourScoresList = input('Enter your Scores List: ') 

in the second input field you have to fill in exactly the name of your scores list you want to use, I called the lists 'scoresList1' and 'scoresList2' as an example.

# initialize DataFrame
scoresDict = pd.DataFrame()
scoresDict['grades'] = ['A', 'B', 'C', 'D', 'E']
scoresDict = scoresDict.set_index(['grades'])

# insert your several scores lists
scoresDict['scoresList1'] = [90,70,60,50,40]            # your scores list
scoresDict['scoresList2'] = [80,60,50,40,30]            # another example scores list
scoresDict # here you can visualize your table

And in the end you can assign the grades for example this way:

for grade in scoresDict.index:
    if yourMark >= scoresDict[yourScoresList][grade]:
        print('Your grade is ', grade)
        break
    if yourMark < scoresDict[yourScoresList]['E']:
        print('You failed')
jana_j
  • 1
  • 2