1 class Radio
(): #1) remove parentheses
2 def __init__(self): #2) constructor must be named __init__
3 self._powerOn = False
4 self._volume = 5
5 self._station = 90.7
6 self._presets = [ 90.7, 92.3, 94.7, 98.1, 105.7, 107.7 ]
7
8 def togglePower(self):
9 self._powerOn = not self._powerOn
10
11 def setPreset(self, ind):
12 self._presets[ind] = self._station #3) _station is an instance variable
13
14 def gotoPreset(self, ind):
15 self._station = self._presets[ind]
16
17 def increaseVolume(self): #4) Indentation of this line must be consistent with rest of the Radio class definition block
18 self._volume = self._volume + 1
19
20 def decreaseVolume(self):
21 self._volume = self._volume - 1
22
23 def increaseStation(self): #5) Must use colon when starting a new block
24 self.station = self.station + .2
25
26 def decreaseStation(self):
27 self.station = self.station - .2
The output is
9 15 a is 3, b is 6Note well that the call foo(2) changes the state of the thing by adding the parameter 2 to both instance variables self._a and self._b. Since they were initially 1 and 4 respectively, they are now 3 and 6 and the returned sum is 9.
Those values remain part of the state of the instance at the onset of the call bar(3). The key difference in this method is that the first two lines assign to local variables a and b. Given the arithmetic, those values will be 6 and 9 respectively, leading to the returned sum of 15 that is displayed on the second line of output.
However, note well that the internal values self._a
and self._b remain 3 and 6 respectively after the
call to bar. So the final command of print
it produces the output
def scale(self, factor):
for k in range(len(self._coords)):
self._coords[k] *= factor
def __add__(self, other):
dim = len(self._coords)
combined = [self._coords[k] + other._coords[k] for k in range(dim)]
return Point(dim, combined)
The simplest approach is to modify the togglePower method so as to read
def togglePower(self):
self._powerOn = not self._powerOn
self._muted = False # will not remain muted after power is toggled