Question about st_dev helper function


#1

Hello, in my final part I tried to write the standard deviation function as follows:


def grades_std_deviation(scores):
  variance=grades_variance(scores)
  std_dev= variance**0.5
  return std_dev

However, it returns float not iterable
However, when I used the function specified in part 8 (return variance**0.5) and run the function as
print grades_std_deviation(grades_variance(grades))
it works fine.
I’m confused because isn’t the grades_variance(grades) a float data as well? why is that working when a float computed from within the function is not? Can you suggest a possible alternative for st_dev function to be independent (as in, can work with the grades input directly)?


#2

yes

so that is good :slight_smile: This as you are told by the instructions

the grades_variance function returns a float when called, yes.

What is your definition of working? Exercise approves or code runs as it should? Maybe the way the exercise validation is setup restricts your options.

There are multiple ways to solve a problem, the question is, which is best? Making separate function allows for better testing (test cases).


#3

oh ok. Thank you for your time


#4

No further questions? You can ask follow up questions :wink: The day i can explain everything perfectly to everyone, will never come


#5

Perhaps when I am less ignorant and inexperienced in these matters. One must know what to ask for after all?:sweat_smile:


#6

asking the right and good questions certainly helps :wink:

what is important to understand, is that putting everything within a single function is a bad idea. Ever heard of spaghetti code? if not, i can highly recommend you to read about it (just google)

but to get experienced you need to ask questions so you understand

now we can separately test the function:

import unittest

def grades_std_deviation(scores):
    return scores**0.5
    
class TestExample(unittest.TestCase):
    def test_integer(self):
        self.assertEqual(grades_std_deviation(16), 4)
        
unittest.main()

this will ensure the right result is returned.

we could also test for errors, which can be handy


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.