[Tutor] return values function

Alan Gauld alan.gauld at btinternet.com
Fri Apr 22 09:32:04 CEST 2011


"Lea Parker" <lea-parker at bigpond.com> wrote

> Write the following functions in the
> program:
>
> * Calc_average: This function should take five test scores as 
> parameters,
> and return the average score.

Note that you are supposed to write a function to do this
not just do it inline.

> *determine_grade; this function should take a single test score as a
> parameter, and return a letter grade for the test.
> The letter grade should be on the following grade scale:
> 90-100: A, 80-89: B, 70-79: C, 60-69: D, <60: F."""
>
> def main():
>    #Get users first test result
>    test1 = int(raw_input('Enter the score for test 1: '))
>    test2 = int(raw_input('Enter the score for test 2: '))
>    test3 = int(raw_input('Enter the score for test 3: '))
>    test4 = int(raw_input('Enter the score for test 4: '))
>    test5 = int(raw_input('Enter the score for test 5: '))

Every time you see repeating code like that you should
ask, can I use a loop? If you store the results in a list,
it then becomes a simple job to read 5 results:

for n in range(5):
    results.append( int(raw_input('Enter the score for test %d:" % 
n')) )

>    #Get the sum of the test results
>    cal_average = sum(test1, test2, test3, test4, test5)/5

This is the average not the sum so your comment is wrong.
It's also where you should be using the function you were
asked to define. The function body would look like the line above.

>    print 'Together your tests average is: ', cal_average
>    print 'Your grade is: ', grade

And here you need to call your other function to work out the grade.
But again you need to call it for each score. You can repeat it
5 times as you did for raw_input or you could use the loop;
method I showed you above.

>    # The sum function to total all tests
> def sum(test1, test2, test3, test4, test5):
>    result = test1 + test2 + test3 + test4 + test5
>    return result

You didn't need this because Python already has a sum()
function that will do this for you.

> def determine_grade(score):
>    #Determine the grade for each score
>    if score <101 and score >89:
>        score = A
>    elif score <90 and score >79:
>        score = B
>    elif score <80 and score >69:
>        score = C
>    elif score <70 and score >59:
>        score = D
>    else:
>        score = F
>    return score

The most serious problem, and it should have thrown
an error, is that the results need to be strings. Otherwise
Python will look for a variable called A,B,C etc.
And no such name exists in your code.

Its not a good idea to use score for both the input parameter
that you test and the result that you return. In this case you
should get away with it because you never use score after
you set it but in general you run the risk of losing access
to the input value after you assign a result to score.

In the comparisons Python allows you to write them like

if 101 > score > 89:  grade = "A"

which is slightly easier to type and read.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list