9/9: there is a bug


#1

Hello.

My code is not working and I do not know why. It is the next one:

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(grades):
average = grades_average(grades)
variance = 0
for score in grades:
variance += ((average - score) ** 2)
variance /= float(len(grades))
return variance

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

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

The results I get are:

100
100
90
40
80
100
85
70
90
65
90
85
50.5
1045.5
80.4230769231
69.0504943648
8.30966271065
None

And I get an error message like the next one: Oops, try again. It looks like the sum of grades was not printed! Make sure you are printing it on its own line. The print_grades(grades) has no print statement before it to avoid a double print command (there is one inside th function).

How could I solve it? I am stuck in this exercise whose code I know is properly working...


#2

I am pretty sure there is a bug when checking the value of the variance. I rewrited the code all over again, the results I get are correct and an error message like the next one pops up (I translated it directly from Spanish):

grades_variance([85, 85, 77, 87, 56, 97, 19, 58, 87, 43, 4, 37, 40, 20]) turned 913.5659340659 instead of the correct value 848.3112244898.

When I enter that sequence the outcome I get for the variance is the correct value 848.3112244898.

How can I skip this page?


#3

It is definitely broken. If you select undo code at the bottom and save and submit you should get an error "grades_std_deviation" is not defined.

instead you get:
Oops, try again. grades_std_deviation(5.5555555556) returned 18.2776094147 instead of the expected: 2.357022604


#4

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 i in scores:
variance += (average - i) ** 2
variance /= len(scores)
return variance

def grades_std_deviation(x):
return x ** 0.5

print grades_variance(grades)


#5

Thank you. Is there a way to skip the exercise and continue with the next one?


#6

Hi, try this code. It worked for me.

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

def print_grades(grades):
    """ prints all numbers in a list"""
    for grade in grades:
        print grade

def grades_sum(grades):
    """ returns the total from a list of numbers"""
    total = 0
    for grade in grades: 
        total += grade
    return total
    
def grades_average(grades):
    """ returns the average from a list of numbers"""
    sum_of_grades = grades_sum(grades)
    # divide the sum by the number of grades and input the result into the average
    average = sum_of_grades / float(len(grades))
    return average

def grades_variance(scores):
    """find the variance"""
    # set variance to 0
    variance = 0
    # call the average from grades_average()
    average = grades_average(scores)
    # start the for loop
    for score in scores:
        variance += (average - score)**2
    #divide the total variance by the number of scores
    variance = variance / len (scores)
    #return the variance as a result
    return variance

def grades_std_deviation(variance):
    """ find the standard deviation of the variance"""
    # compute the standard deviation
    stdev  = variance ** 0.5
    # return the standard deviation
    return stdev
    

# put all the print statements into a function!
def print_class_grades (grades):
    """ print out all the grades, the sum of grades, average grades, variance, and standard deviation"""
    # print all grades
    print print_grades (grades)
    # print sum of grades
    print grades_sum (grades)
    # print average grades
    print grades_average (grades)
    # print variance of grades
    print grades_variance(grades)
    # print standard deviation
    print grades_std_deviation(grades_variance(grades))
    
# print the grades
print_class_grades(grades)

9. Review - unsupported operand ** with 'list' and 'float' error?
#7

I have the same problem, this is my code.

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

def print_notas(calificaciones):
    for calificacion in calificaciones:
        print calificacion

def notas_sum(calificaciones):
    total = 0 
    for calificacion in calificaciones: 
        total += calificacion
    return total

def promedio_notas(calificaciones):
    suma = notas_sum(calificaciones)
    promedio = suma / float(len(calificaciones))
    return promedio
 
def varianza_notas(calificaciones):
    promedio = promedio_notas(calificaciones)
    varianza = 0
    for calificacion in calificaciones:
        varianza += (promedio - calificacion)**2
    return varianza / len(calificaciones)

def calificaciones_std_desviacion(varianza) :
    return varianza ** 0.5

print print_notas(notas)
print notas_sum(notas)
print promedio_notas(notas)
print varianza_notas(notas)
varianza = varianza_notas (notas)
print calificaciones_std_desviacion(varianza)

This is the result:

100
100
90
40
80
100
85
70
90
65
90
85
50.5
None
1045.5
80.4230769231
334.071005917
18.2776094147
None

And this is the error message:

¡Uy! Probá otra vez. Parece que no se imprimieron todas las notas. Asegurate de estar mostrando en pantalla cada una en una línea.


#8

even with this code i get the error. There is a bug.


#9

This really helped....
Thanks a lot!!!!


#10

This is my code. It´s works.

notas = [100, 100, 90, 40, 80, 100, 85, 0, 90, 65, 90, 85, 50.5]

def print_calificaciones(calificaciones):
    for calificacion in calificaciones:
        print calificacion
        print

def notas_sum(calificaciones):
    total = 0 
    for calificacion in calificaciones: 
        total += calificacion
    return total

def promedio_notas(calificaciones):
    suma = notas_sum(calificaciones)
    promedio = suma / float(len(calificaciones))
    return promedio
 
def varianza_notas(calificaciones):
    promedio = promedio_notas(calificaciones)
    varianza = 0
    for calificacion in calificaciones:
        varianza += ((calificacion - promedio) ** 2) / len(calificaciones)
    return varianza

def calificaciones_std_desviacion(varianza):
    return varianza**0.5
    
varianza = varianza_notas(notas)

print notas  # <------ **PRINT LIST. DON´T CALL FUNTION**
print "-----------------"
print notas_sum(notas)
print "-----------------"
print promedio_notas(notas)
print "-----------------"
print varianza
print "-----------------"
print calificaciones_std_desviacion(varianza)

#11

Hola este es mi codigo, funciona solo hay que imprimir al principio la lista.

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

def print_calificaciones(calificaciones):
for calificacion in calificaciones:
print calificacion

def notas_sum(calificaciones):
total = 0
for calificacion in calificaciones:
total += calificacion
return total

def promedio_notas(calificaciones):
suma = notas_sum(calificaciones)
promedio = suma / float(len(calificaciones))
return promedio

def varianza_notas(calificaciones):
promedio = promedio_notas(calificaciones)
varianza = 0
for calificacion in calificaciones:
varianza += (promedio - calificacion)**2
return varianza / len(calificaciones)

def calificaciones_std_desviacion(varianza):
return varianza ** 0.5
varianza = varianza_notas(notas)
print notas
print "el total de calificaciones",len(notas)
print "la suma de las calificaciones",notas_sum(notas)
print "la calificacion promedio",promedio_notas(notas)
print "la varianza", varianza_notas(notas)
print "la desviacion estandar", calificaciones_std_desviacion(varianza)


#12

muchas gracias =) no me dejaba seguir porque me faltaba la impresion del tamaño del array


#13

Gracias, ya empezaba a volverme loco,


#14

Gracias, no podía avanzar :smiley:


#15

yeah thanks it worked there must be a bug inmy code and i'm abit lazy at it is taking too much time