-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlec8prob3-.py
32 lines (26 loc) · 1.02 KB
/
lec8prob3-.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# lec8prob3-.py
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Lecture 8, problem 3
# This code raises a ZeroDivisionError exception for the following call:
# FancyDivide([0, 2, 4], 0)
# Your task is to change the definition of SimpleDivide so that the call does
# not raise an exception. When dividing by 0, FancyDivide should return a list
# with all 0 elements. Any other error cases should still raise exceptions.
# You should only handle the ZeroDivisionError.
def FancyDivide(list_of_numbers, index):
denom = list_of_numbers[index]
return [SimpleDivide(item, denom)
for item in list_of_numbers]
# Original code for SimpleDivide
# def SimpleDivide(item, denom):
# return item / denom
# Fixed code for SimpleDivide that handles the ZeroDivisionError
def SimpleDivide(item, denom):
try:
return item / denom
except ZeroDivisionError, e:
emptyList = []
return 0
# Enclosed in print statement to see result
print (FancyDivide([0, 2, 4], 0))