Assignment 3 Burhanuddin Sikandar Roll No:-1511
Assignment 3 Burhanuddin Sikandar Roll No:-1511
BURHANUDDIN SIKANDAR
Roll No :- 1511
We’ve learned about many Python data structures (strings, lists, tuples, dictionaries). For
both “mutable” and “immutable”, please give a short (5 words or fewer) definition, and
then list what data structure(s) have that characteristic.
Mutable:
Immutable:
The following set of instructions were given to Ben Bitdiddle, and he produced the code
below. Find at least three bugs he made, and say how to fix them.
Instructions: Write a negate function that takes a number and returns the negation of that
number. Also write a large num function that takes a number, and returns True if that
number is bigger than 10000, and False otherwise. Additionally, write some code to test
your functions.
def negate(num):
return -num
def large_num(num):
res = (num > 10000)
negate(b)
neg_b = num
print ’b:’, b, ’neg_b:’, neg_b
big = large_num(b)
print ’b is big:’, big
Bugs:
1. <textarea name="a[3-2-1]"></textarea>
2. <textarea name="a[3-2-2]"></textarea>
3. <textarea name="a[3-2-3]"></textarea>
1
Exercise 3.9 – Mystery Program
Ben next turned in the following uncommented code to the 6.189 LAs. Help us figure
out what it does!
1 print "Think of a number between 1 and 100, but don’t tell me what you
choose." 2 min_n = 1
3 max_n = 100
4 right_answer = False
5
6 while not right_answer:
7 mid_n = (max_n + min_n + 1)/2
8 answer = raw_input(’Is it ’ + str(mid_n) + ’? ’)
9 if answer[0] == ’y’:
10 right_answer = True
11 elif answer.startswith(’higher’):
12 min_n = mid_n + 1
13 elif answer.startswith(’lower’):
14 max_n = mid_n - 1
15 else:
16 print "Sorry, I don’t understand your answer."
17
18 print ’Woohoo! I got it!’
1. The while loop exits when the variable right answer is True. What will cause
right answer to be true?
<textarea name="a[3-3-1]"></textarea>
2. How many times will the program print out 'Woohoo! I got it!'?
<textarea name="a[3-3-2]"></textarea>
5. If the program gets the response 'higher', what does that tell it about its guess?
<textarea name="a[3-3-5]"></textarea>
6. What are the variables min n, max n and mid n used for?
<textarea name="a[3-3-6]"></textarea>
2
Exercise 3.10 – Intro to Object Oriented Programming
attribute?
3. If you have an object instance, obj, and you want to call its do something() method
(assuming it has one), how would you do this? (write the line of code you would
use)
obj.do_something()
3.11 – Understanding Objects
1. Write a class called Address that has two attributes: number and street name. Make
sure you have an init method that initializes the object appropriately.
class Address:
def __init__(self, street, num):
self.street_name = street
self.number = num
class Clock:
def print_time(self):
time = ’6:30’
print self.time
clock = Clock(’5:30’)
clock.print_time()
(a) What does the code print out? If you aren’t sure, create a
Yes, because we printed out the attribute self.time, not the local variable time.
class Clock:
clock = Clock(’5:30’)
clock.print_time(’10:30’)
(a) What does the code print out? If you aren’t sure, create a Python
file and run it.
10:30
(b) What does this tell you about giving parameters the same name as
object attributes?
They are needlessly confusing. It is less confusing if you give parameters, local
variables,
and attributes different names.
4
4. Consider the following code:
class Clock:
def print_time(self):
print self.time
boston_clock = Clock(’5:30’)
paris_clock = boston_clock
paris_clock.time = ’10:30’
boston_clock.print_time()
(a) What does the code print out? If you aren’t sure, create a Python file and run it.
10:30
(b) Why does it print what it does? (Are boston clock and paris clock different
objects? Why or why not?)
boston_clock and paris_clock are two names for the same object. This is called
"aliasing."