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!
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
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.
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.