This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
forked from slgraff/edx-mitx-6.00.1x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec8prob2-.py
91 lines (79 loc) · 2.33 KB
/
lec8prob2-.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# lec8prob2-.py
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
# Lecture 8, problem 2
# Below are some short Python programs. For each program, answer the
# associated question.
#
# Try to answer the questions without running the code. Check your answers,
# then run the code for the ones you get wrong.
#
# The function in the following questions takes a list of integers numbersand
# a position index, and divides each entry in the list of numbers by the value
# at entry index.
# There are five functions named FancyDivide. Uncomment the one you wish to
# use along with the calls to it to test
# def FancyDivide(numbers,index):
# try:
# denom = numbers[index]
# for i in range(len(numbers)):
# numbers[i] /= denom
# except IndexError, e:
# print "-1"
# else:
# print "1"
# finally:
# print "0"
# def FancyDivide(numbers, index):
# try:
# denom = numbers[index]
# for i in range(len(numbers)):
# numbers[i] /= denom
# except IndexError, e:
# FancyDivide(numbers, len(numbers) - 1)
# except ZeroDivisionError, e:
# print "-2"
# else:
# print "1"
# finally:
# print "0"
def FancyDivide(numbers, index):
try:
try:
denom = numbers[index]
for i in range(len(numbers)):
numbers[i] /= denom
except IndexError, e:
FancyDivide(numbers, len(numbers) - 1)
else:
print "1"
finally:
print "0"
except ZeroDivisionError, e:
print "-2"
# def FancyDivide(list_of_numbers, index):
# try:
# try:
# raise Exception("0")
# finally:
# denom = list_of_numbers[index]
# for i in range(len(list_of_numbers)):
# list_of_numbers[i] /= denom
# except Exception, e:
# print e
# def FancyDivide(list_of_numbers, index):
# try:
# try:
# denom = list_of_numbers[index]
# for i in range(len(list_of_numbers)):
# list_of_numbers[i] /= denom
# finally:
# raise Exception("0")
# except Exception, e:
# print e
# Calls to FancyDivide
# FancyDivide([0, 2, 4], 1)
print ''
FancyDivide([0, 2, 4], 4)
print ''
# FancyDivide([0, 2, 4], 0)