0

When given a list I'd like it to return the average, but when given nothing I'd like it to return default "None". Currently "print(averagefunction())" returns error of lacking argument.

def averagefunction(variablelist):
    numerator = 0
    denominator = 0
    for item in variablelist:
        numerator = numerator + item
        denominator += 1
    average = numerator/denominator
    if denominator > 0:
        return average
print(averagefunction([5,6,10]))
print(averagefunction())
Piegoose
  • 43
  • 6
  • How about using an `if` statement to achieve this? You can do it right at the top of the function. – John Zwinck Dec 09 '18 at 01:30
  • I've tried that, but when I call the function without a argument it returns an error. I'm trying to get around the error. – Piegoose Dec 09 '18 at 01:31
  • Oh, then see here for how to make functions taking optional arguments: https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments/9539945 – John Zwinck Dec 09 '18 at 01:35

0 Answers0