ayokona
ayokona

Reputation: 27

Python: Writing a greater than and less than form

So I am writing a code about with an elif statements like if a student get a score below 60, they will get an F. if they got a score atleast 60 but below 70, they will get a D. But reading the problem suddenly got me confuse on how to use the less than and greater than again;-; i already tried to do it but i am not sure if i am correct. so here's the problem;

and below this, is my i-tried-to-do-it

Upvotes: 0

Views: 3315

Answers (6)

Jan Christoph Terasa
Jan Christoph Terasa

Reputation: 5935

You can use a dict to map the lower bounds to each grade, then just iterate over that dict to find the grade:

def print_grade(score):
    """
    the keys in grades must be monotonically decreasing
    """
    grades = {
        90: 'A',
        80: 'B',
        70: 'C',
        60: 'D',
         0: 'F'
    }
    
    for k, v in grades.items():
        if score >= k:
            print(v)
            break

With this you can more easily replace the grade levels, without changing the actual logic below, providing a more clean separation between "algorithm" and "data". This can also be done using tuples or lists. Such a separation can later allow you to pass grades as a parameter from outside the function, e.g. if you have another piece of code which calculates the grading map.

Upvotes: 1

tdelaney
tdelaney

Reputation: 77337

Since an elif clause will only run if all previous if and elif are False, you don't have to check ranges. As long as you test from highest to lowest, its just

score = 77

if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
elif score >= 60:
    print("D")
else:
    print("F")

If the if clauses seem repetitive, you could create a list of bounds and spin through them in a loop. But for this case, the if's will be a bit faster in execution.

grade_levels = ((90, "A"), (80, "B"), (70, "C"), (60, "D"))
def get_grade(score):
    for lower_bound, grade in grade_levels:
        if score >= lower_bound:
            return grade
    return "F"

print(get_grade(100))
print(get_grade(80))
print(get_grade(44))

Upvotes: 1

Tom Ron
Tom Ron

Reputation: 6181

The condition 80<=90 will always be true since you compare constant integers you should compare the variable.

 if score >=90:
    print('A')
elif score>=80:
    print('B')
elif score>=70:
    print('C')
elif score>=60:
    print('D')
else:
    print('F')

Upvotes: 0

Antricks
Antricks

Reputation: 169

You'll need to compare the score to constants, also mind the indentation:

if score >= 90:
    print('A')
elif score >= 80:
    print('B')
elif score >= 70:
    print('C')
elif score >= 60:
    print('D')
else:
    print('F')

Upvotes: 1

ghchoi
ghchoi

Reputation: 5156

How about this approach:

print('FFFFFFDCBAA'[score // 10])

In Python 3, score // 10 will give you an integer index! For example, 75 // 10 = 7.

Upvotes: 3

Lakshan Costa
Lakshan Costa

Reputation: 643

Here you go. You should add indentation or you will get an error.

marks = int(input("Enter Marks: "))
if marks>=90:
    print("A")
elif marks>80:
    print("B")
elif marks>70:
    print("C")
elif marks>60:
    print("D")
else:
    print("F")

Upvotes: 0

Related Questions