0% found this document useful (0 votes)
84 views7 pages

Assignment 3 Burhanuddin Sikandar Roll No:-1511

The document contains Ben Bitdiddle's assignment on exercises related to mutability, finding bugs, and an intro to object oriented programming in Python. For the mystery program exercise, the student identified that: 1) 'right answer' becoming True would cause the while loop to exit; 2) It would print "Woohoo! I got it!" once; 3) The variable 'answer' is used to get user input on guesses; 4) It would understand responses of "y", "higher", or "lower"; 5) "higher" means the guess is too low; and 6) The variables are used to iteratively narrow the range of possible numbers based on the user's responses, implementing a binary search algorithm.

Uploaded by

Barry Allen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views7 pages

Assignment 3 Burhanuddin Sikandar Roll No:-1511

The document contains Ben Bitdiddle's assignment on exercises related to mutability, finding bugs, and an intro to object oriented programming in Python. For the mystery program exercise, the student identified that: 1) 'right answer' becoming True would cause the while loop to exit; 2) It would print "Woohoo! I got it!" once; 3) The variable 'answer' is used to get user input on guesses; 4) It would understand responses of "y", "higher", or "lower"; 5) "higher" means the guess is too low; and 6) The variables are used to iteratively narrow the range of possible numbers based on the user's responses, implementing a binary search algorithm.

Uploaded by

Barry Allen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment 3

BURHANUDDIN SIKANDAR
Roll No :- 1511

Exercise 3.7 – Mutability

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:

Exercise 3.8 – Finding Bugs

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>

3. What are we using the variable answer for?


<textarea name="a[3-3-3]"></textarea>
4. The program makes a guess in line 8. What user responses will be understood
by the program after it makes its guess?
<textarea name="a[3-3-4]"></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>

This is an example of binary search, a simple but important algorithm in computer


science. If you’re curious, or confused, read the Wikipedia article on binary search to
find out more and get a good explanation of what’s going on here.

2
Exercise 3.10 – Intro to Object Oriented Programming

1. What is the difference between a local variable and an object’s

attribute?

In contrast to attributes, which can be used throughout an object independent of the


method in which they were first defined, local variables are variables that are utilised
just within a method. However, the __init__ method is typically where we want to
define attributes.

2. What method is called when the object is created?


__init__ (self, ...)

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

2. Consider the following code:

class Clock:

def __init__(self, time):


self.time = time

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

Python file and run it.


5:30

(b) Is that what you expected? Why?

Yes, because we printed out the attribute self.time, not the local variable time.

3. Consider the following code:

class Clock:

def __init__(self, time):


self.time = time

def print_time(self, time):


print time

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 __init__(self, time):


self.time = time

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."

You might also like