5/9 An Average question: when to define a function & when to just print result


#1

I realize that I would be able to use the defined function at a later time (if necessary), but is there any other reason why I shouldn't just print the result withouth aving to go through extra lines of code? What are other disadvantages of solving the problem like this?

Would this be an example of 'hard coding' answers as opposed to generating dynamic algorithms to perform at will?

I am still trying to figure out if these lessons are preparing me for best-practice scenarios or if I should be more concerned with picking up on basic functionality.

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
    total = 0
    for i in scores:
        total = total + i
    return total
        
print grades_sum(grades)
 
def grades_average(grades):
    average = grades_sum(grades)/float(len(grades))
    return average
    
print grades_average(grades)

Why not just do this?

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
    total = 0
    for i in scores:
        total = total + i
    return total
        
print grades_sum(grades)
 
print grades_sum(grades)/float(len(grades))

#2

That would be your best bet. We are not aiming at perfection as much as basic understanding of syntax and mechanics. You will find lots of discussion in the forums concerning best practices, something for you to augment your learning. Here, it is more important that we are able to follow instructions and complete each lesson as close as possible to expectations.


#3

Right, basic functionality is pretty much the goal with most of the stuff on codecademy. In this case teaching you how to define and use a function, even though you will only use it once. I'm pretty sure in your own programs there wouldn't be any point in defining a function just to use it once. Although there are lambdas which are single use functions and I don't fully understand the purpose of using them, but there you go.