Super Simple Python: Grader

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple grader in under 15 lines of Python!

We don’t need to import any libraries for this function. For this episode we’re going to simply be building some functions out and using them like we did in the Unit Convertor, and the Calculator.

Grader Function in Python

Let’s pretend like we’re lucky and we go somewhere with a 10 point scale. If you’re in the UK I’m looking at you. When I went to school (in America) we had a 7 point system. I was a straight A student anyway, but it’s still bullshit to me lol.

Anyway, let’s get started by creating a grader function that takes one parameter. We expect our score parameter to be a float. In our function, we can just go down the list and return A, when the score is above 90, B when the score is about 80, and so on until we get to F. Note that we don’t have to build an upper bound on any of these grades because we go down in order and return when we find what we need.

def grader(score: float):
    if score > 90.0:
        return 'A'
    if score > 80.0:
        return 'B'
    if score > 70.0:
        return 'C'
    if score > 60.0:
        return 'D'
    else:
        return 'F'

Testing the Grader Function Out

Now that we’ve built the grader, let’s test it out. All we are going to do to test it is print out the return value from different numbers. Notice how I included some integers in the test. This is just to illustrate a point. Even though our function expects floats, we can pass integers and Python will interpret it correctly!

print(grader(91.0))
print(grader(88.5))
print(grader(71))
print(grader(89.9))
print(grader(64))

We should expect to see the output: A, B, C, B, D from above. When we run our program we get an output like the image below.

As expected, we got A, B, C, B, D.

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: