Unit 9: exam statistics


#1

Hi!
I stuck at UNIT 9: EXAM STATISTICS:
This is my code

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

def print_grades(scores):
    for score in scores:
        print score

print_grades(grades)

def grades_sum(scores):
    total = 0
    for score in scores: 
        total += score
    return total

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

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

print grades_variance(grades)

def grades_std_deviation(variance):
    return variance ** 0.5

variance = grades_variance(grades)
print grades_std_deviation(variance)

and this is output

100
100
90
40
80
100
85
70
90
65
90
85
50.5
1045.5
Traceback (most recent call last):
  File "python", line 30, in <module>
  File "python", line 24, in grades_variance
  File "python", line 19, in grades_average
TypeError: 'float' object is not callable

can't get what's wrong ((( help me, please


#2

Issue with indentation basically. See below:

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

#3

do you really mean i should call function inside it's own definition?


#4

hmm, i did that, but get the same output


#5

You know, python uses indentation to specify where what does what. Please post full code after considering my initial suggestion.

Thank you! :slight_smile:


#6

code:

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

def print_grades(scores):
    for score in scores:
        print score

print_grades(grades)

def grades_sum(scores):
    total = 0
    for score in scores: 
        total += score
    return total

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

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

print grades_variance(grades)

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

output:

100
100
90
40
80
100
85
70
90
65
90
85
50.5
1045.5
Traceback (most recent call last):
  File "python", line 30, in <module>
  File "python", line 24, in grades_variance
  File "python", line 19, in grades_average
TypeError: 'float' object is not callable

#7

When I saw that the error was from previous lines, I did a revisit and you had some mix-up. The standard deviation function created has dependency on about all previous.

Scan through and see my edits.

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/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)

#8

All works fine now!
Thank you! :slight_smile:


#9

You're welcome.

Have fun coding! :slight_smile:


#10

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