[Tutor] (no subject)

bob bgailer at alum.rpi.edu
Sun Sep 25 03:30:15 CEST 2005


At 03:29 PM 9/24/2005, Goofball223 at wmconnect.com wrote:
>Hello
>
>How would I get the following program to accept inputs of exam scores from 
>0-100 with A being 100-90, B being 89-80, C being 79-70, D being 69-60, 
>and F being everything less than 60?

Many solutions are available. One could use an if statement:

if g >= 90: s = 'A'
elif g >= 80: s = 'B'
...
else: g = 'F'

although that can be hard to maintain.

Or associate the lower limit with the data and search using a loop:

scores = [("A",90), ("B",80), ("C".70), ("D",60),("F",0) ]
for letter, number in scores:
     if g >= number:break
print "The score of your exam is", letter

This separates the data from the program structure and becomes much easier 
to maintain / extend, apply to new situation.

If you are using a database then you could store these value pairs in a 
table and use SQL to retrieve the desired letter.

When the number ranges have a nice progression, you can reduce the number 
to an index:

print "The score of your exam is", "FFFFFEDCBAA"[g/10] # this is simpler 
code but harder to read/maintain.

Or - knowing that chr(65) = "A", chr(66) = "B" you could convert the number 
to be in the range 65..70 and use chr()

>import string

You do not refer to the string module, so there is no need to import it. 
Also be ware that it will eventually go away. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050924/1f6d1b80/attachment.htm


More information about the Tutor mailing list