ursamajor: Tajel on geeks (geeks: love them)
I'm working my way through How to Think Like a Computer Scientist, interactive Python edition. I have also recently gotten sucked into reading Alternity, and recently, the fifth-years have all gotten their OWL results.

So when Exercise 3 in the chapter I'm on looked like the following:

Write a function which is given an exam mark, and it returns a string — the grade for that mark — according to this scheme:

Mark Grade
>= 90 A
[80-90) B
[70-80) C
[60-70) D
< 60 F


The square and round brackets denote closed and open intervals. A closed interval includes the number, and open interval excludes it. So 79.99999 gets grade C , but 80 gets grade B.

Let xs = [83, 75, 74.9, 70, 69.9, 65, 60, 59.9, 55, 50, 49.9, 45, 44.9, 40, 39.9, 2, 0]

Test your function by printing the mark and the grade for all the elements in this list.


Of course my response is going to look like:


def grader (x):
    if x >= 90:
        return "Outstanding"
    elif x >=80 and x <90:
        return "Exceeds Expectations"
    elif x >=70 and x <80:
        return "Acceptable"
    elif x >=60 and x <70:
        return "Pass"
    elif x >= 50 and x < 60:
        return "Dismal"
    else:
        return "Troll"

gradesum = 0
counter = 0
avggrade = 0
for grade in [83, 75, 74.9, 70, 69.9, 65, 60, 59.9, 55, 50, 49.9, 45, 44.9, 40, 39.9, 2, 0]:
    print(grader(grade), "\t", grade)
    gradesum = gradesum + grade
    counter = counter + 1
    avggrade = gradesum/counter
print("\r", "\t", grader(avggrade), "(overall average)", "\t", int(avggrade))


... yes, I went all Ravenclaw and made it output the average grade at the end. :)

*

Separately, [community profile] zero2perl now exists, thanks to [personal profile] zarhooie, and if you like word ladders like me (and are down with the use of proper nouns and British spellings):

ZERO
HERO
HERB
KERB
KERN
PERN
PERL

See? Six simple steps. :)

Profile

ursamajor: people on the beach watching the ocean (Default)
she of the remarkable biochemical capabilities!

October 2016

S M T W T F S
      1
2 3456 78
9 10 1112 131415
16 17 18 19202122
2324 2526 2728 29
3031     

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 24th, 2017 03:31 pm
Powered by Dreamwidth Studios