Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me identify the error in my python code? I 'm receiving the following error: ZeroDivisionError: division by zero Right now I have

Can you help me identify the error in my python code? I'm receiving the following error: ZeroDivisionError: division by zero 

Right now I have the following:

def average_grade(adict):

"""

Returns the average grade among all students.

The dictionary adict has netids for keys and numbers 0-100 for values.

These represent the grades that the students got on the exam.This function

averages those grades and returns a value.

Examples:

average_grade({'wmw2' : 55, 'abc3' : 90, 'jms45': 86}) returns (55+90+86)/3 = 77

average_grade({'wmw2' : 55}) returns 55

average_grade({}) returns 0

Parameter adict: the dictionary of grades

Precondition: adict is dictionary mapping strings to ints

"""

n=0

sum=0

for key in adict:

sum+=adict[key]

n+=1

return sum/n

These are the tests:

def test_average_grade():

"""

Test procedure for function average_grade().

"""

print('Testing average_grade()')

grades = {'wmw2': 55, 'abc3': 90, 'jms45': 86}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(77.0,result)

introcs.assert_equals(orignl,grades)

grades = {'abc123': 0,'abc456':65,'jms457':50}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(38.333,result)

introcs.assert_equals(orignl,grades)

grades = {'abc123': 0,'abc456':65,'jms457':50,'jms123':60,'xyz123':70}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(49.0,result)

introcs.assert_equals(orignl,grades)

grades = {'abc123': 0,'abc456':65,'jms457':50,'jms123':60,'xyz123':70,'xyz456':80,'wmw4':90}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(59.286,result)

introcs.assert_equals(orignl,grades)

grades = {'abc123': 0,'abc456':65,'jms457':50,'jms123':60,'xyz123':70,'xyz456':80,'wmw4':90,'wmw5':100}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(64.375,result)

introcs.assert_equals(orignl,grades)

grades = {'abc123': 0,'abc456':65,'jms457':50,'jms123':60,'xyz123':70,'xyz456':80,'wmw4':90,'wmw5':100,'tor3':88}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(67.0,result)

introcs.assert_equals(orignl,grades)

grades = {'wmw2' : 55, 'abc3' : 90}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(72.5,result)

introcs.assert_equals(orignl,grades)

grades = {'abc3' : 90}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(90.0,result)

introcs.assert_equals(orignl,grades)

grades = {}

# Snapshot table to make sure we do not modify

orignl = copy.deepcopy(grades)

result = funcs.average_grade(grades)

introcs.assert_floats_equal(0.0,result)

introcs.assert_equals(orignl,grades)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions