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)