(2 * 3) + (4 * 5)Note that parentheses are optional, but not necessary here, since the * operator has higher precedence (will bind its operands earlier) than the + operator, so the Python expression 2*3+4*5 means what we think it should mathematically.
def square(x): return x*x
if 3 > 4: 5 else: 6
Question 2: By examining the code in adventure.py and understanding what it means to inherit methods in subclasses, explain what happens when:
def is_ownable(self): return Trueso when is_ownable() is invoked on an OwnableObject, the result is True.
def is_ownable(self): return False # overridden by some subtypesso the result of is_ownable() when invoked on a MobileObject is False.
class Professor (Lecturer): def profess(self, s): self.lecture("It is intuitively obvious that " + s)Note that this is better than using,
self.say("It is intuitively obvious that " + s + " - you should be taking notes")since it avoids the duplication from the lecture method.
class Dean (Professor): def say(self, s): Professor.say(self, s + " Please give money, we are in debt!")
Some of the students in Charlottansville have a strange habit of getting undressed and running around the Green, so the Student class needs an instance variable dressed that indicates whether or not the student is clothed. Initially, all students are dressed, so the dressed variable is initialized to True. So, your Student class needs to override the __init__ constructor, similarly to how the Person class overrides the __init__ constructor of MobileObject.
In addition, your Student class should implement three methods:
class Student(Person): def __init__(self, name): Person.__init__(self, name) self.dressed = True def get_undressed(self): if is_dressed(): self.dressed = False self.say("Brr! It's cold!") else: self.say("I have nothing left to remove!") def get_dressed(self): if !is_dressed: self.say("I feel much better now.") self.dressed = True else: self.say("I'm already dressed, thank you."); def is_dressed(self): return self.dressed
def arrest(self, person): if self.location != person.location: self.say("I cannot arrest " + person.name + " because she is at " + person.location.name) else: self.say(person.name + ", you are under arrest!) self.say("You have the right to remain silent, call methods, and mutate state.") person.move(self._jail)
def tick(self): foundAny = False for p in self.location.get_things(): if isinstance(p, Student): if not p.is_dressed(): foundAny = True self.arrest(p) if not foundAny: self.say("No one to arrest here") Person.tick(self)Another strategy is to use filter, which is built-in to Python and similar to the list-filter method from the book, and map, which is similar to list-map. This is based on Anjali Nanda and Harry Peppiatt's solution:
def tick(self): students = filter(lambda s: isinstance(s, Student), self.location.get_things()) undressedstudents = filter(lambda t: t.is_dressed() == False, students) if len(undressedstudents) == 0: self.say("Its lonely here...") Person.tick(self) else: map(lambda undressedstudent: self.arrest(undressedstudent), undressedstudents)We could make this shorter by combining both tests into a single filter:
def tick(self): streakers = filter(lambda s: isinstance(s, Student) and s.is_dressed(), self.location.get_things()) if len(streakers) == 0: self.say("Its lonely here...") Person.tick(self) else: map(lambda s: self.arrest(s), streakers)