9.Review


#1



It shows that I bit off from the code. I am trying figure out what mistake i made

Traceback (most recent call last):
File "python", line 38, in
File "python", line 30, in grades_std_deviation
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'float'


Oops, try again. Your code looks a bit off. Feel free to peek back at earlier exercises if you need a refresher! Take a look at the console window for the error message from the interpreter.


I expect to output tall grades, sum of grades, average of grades, variance, standard deviation


print print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
print grades_std_deviation(grades)


#2

Hi @systemcoder22323,

Since you have not posted all your code, we cannot know for certain what the problem is. However, if your functions have all been written according to the exercise specifications, you should not be passing grades directly to the grades_std_deviation function. Instead, you need to pass it the variance, which can be computed as the result of passing grades to the grades_variance function. That can be done as follows ...

print grades_std_deviation(grades_variance(grades))

If you would rather do it in two steps, this will work ...

variance = grades_variance(grades)
print grades_std_deviation(variance)

#3

here is my full code

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

def print_grades(grades):
for grade in grades:
print grade

def grades_sum(grades):
total = 0
for grade in grades:
total += grade
return total

def grades_average(grades):
sum_of_grades = grades_sum(grades)
average = sum_of_grades / float(len(grades))
return average

def grades_variance(scores):
average = grades_average(scores)
variance =0
for score in scores:
variance += (average - score) ** 2
result = variance / float(len(scores))
return result

print grades_variance(grades)

def grades_std_deviation(variance):
return variance ** 0.5
variance = grades_variance(grades)
print grades_std_deviation(variance)

print print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
print grades_std_deviation(grades)


#4

Just replace the final line with these two lines, so that your program computes and passes variance to the grades_std_deviation function.

variance = grades_variance(grades)
print grades_std_deviation(variance)

#5

I try that it shows like this

Oops, try again. It looks like not all grades were printed! Make sure you are printing each one on their own line.


#6

Hi @systemcoder22323,

Start with the original code that you posted, then make sure that after your grades_std_deviation function definition, you have only the following lines of code ...

print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
variance = grades_variance(grades)
print grades_std_deviation(variance)

The above statements will display all the output that is specified in the instructions.

It is important to remove this line anywhere it occurs in your code. If executed, it will raise an error ...

print grades_std_deviation(grades)

#7

Ok I made the change now it clear
Here we use grades_std_deviation(variance) why?


#8

That is because the grades_std_deviation function is defined as follows ...

def grades_std_deviation(variance):
    # Use the variance to compute the standard deviation.
    return variance ** 0.5

Note that a comment has been added to it.

To compute the standard deviation, the function takes a variance as an argument, and it returns the square root of that variance.

A programmer could, of course, write another standard deviation function that accepts a list of numbers as an argument, and computes the standard deviation based on those numbers. However, that differs from what is specified in the instructions for this exercise.


#9

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