[Tutor] bug in exam score conversion program
Brian C. Lane
bcl at brianlane.com
Sat Oct 4 16:31:45 CEST 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
David wrote:
> Hello!!
>
> I just completed exercise 7 (chapter 4) in Zelle's book:
> "A certain CS professor gives 100-point exams that are graded on the
> scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that
> accepts an exam score as input and prints out the corresponding grade."
>
Just to throw in another method, I tend to use tables of for problems
like this. The requirements usually change so its easier to modify later:
# min, max, grade
grades = [ (90,100,'A'),
(80, 89,'B'),
(70, 79,'C'),
(60, 69,'D'),
( 0, 59,'F'),
]
def getGrade(score):
"""
Return a letter grade based on a score
"""
for g in grades:
if (score <= g[1]) and (score >= g[0]):
return g[2]
- --
- ---[Office 71.6F]--[Outside 55.4F]--[Server 107.9F]--[Coaster 71.7F]---
- ---[ WSF KITSAP (366772980) @ 47 34.7811 -122 27.7554 ]---
Software, Linux, Microcontrollers http://www.brianlane.com
AIS Parser SDK http://www.aisparser.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
Comment: Remember Lexington Green!
iD8DBQFI535RIftj/pcSws0RAldqAJ9yKYSyDArc/LZ6G47SwxUq4z8yAACgioyx
b9WnwDEQe8hSOuYbKuKo9sY=
=7lCV
-----END PGP SIGNATURE-----
More information about the Tutor
mailing list