 Help re: Part of the Whole (Learning Python 2)

So, the code for the function is posted below:

def average(numbers):
total = sum(numbers)
total = float(total)

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

if score >= 90:
return "A"
elif score >=80:
return "B"
elif score >=70:
return "C"
elif score >=60:
return "D"
else:
return "F"

My questions have to do with the terms “numbers” and “score” within the functions being defined: since we have not defined “numbers” or “score”, how does the computer know what they are? Are these terms already part of the Python language?

score and numbers are parameters, they get there value from the argument at function call:

# defining function with parameter
def get_average(student):

# calling function with argument, the value for the parameter, lloyd in this case
get_average(lloyd)

So parameters act as placeholders until there value from the argument(s) at function call

1 Like

Except with the function get_average(student), we already defined the list of students at the beginning of the exercise (which is not included in this panel of code, and which I assume is part of the overall code being run anyway…?)
That way the computer knows to look at the list of students for “Lloyd” or “Alice” and select their particular set of homework grades…isn’t that correct?
So by that logic, “score” should be defined somewhere as:
score = get_average_student) or something like that, no?

You got it completely wrong.

parameters don’t need a value until you call the function, and then you provide argument(s) for the parameter(s):

def example(par):
print(par)

example('hello world')
example('foo')
example('bar')
example('baz')

in the example above i define a function with a parameter named par, then i make a bunch of function calls in which i pass different string value/argumens to the parameter.

so for example here:

example('hello world')

hello world gets assigned to par (under the hood):

par = 'hello world'
1 Like

OK, understood.
One more question:
Is it necessary to put the return function on a separate line as in:
if score >= 90:
return “A”
elif score >=80:
return “B”

or can it just as easily be written on a single line, as in:

if score >=90: return “A”

?

python has a style guide:

https://www.python.org/dev/peps/pep-0008/

which recommends separate lines. A style guide is not mandatory, but its good practice to follow the style guide

1 Like

ok, fair enough! 