PY. TEMPLATE BELOW : \"\"\" This is your template. Implement all solutions in th
ID: 3753271 • Letter: P
Question
PY. TEMPLATE BELOW :
"""
This is your template. Implement all solutions in the appropriate
function / class. You are encouraged to implement you own helper functions
as
needed with a short (one-line) description of their purpose.
Hint: copy and paste the function descriptor format from lab1.py to save
time.
"""
class Robot:
""" implement this class below with appropriate function descriptors
"""
def __init__(self, safety_word, pos):
self.pos_x = pos[0]
self.pos_y = pos[1]
self.safety_word = safety_word
def beep():
pass
def raise_alarm(self, n):
pass
def emote(self):
pass
def move(self, vector):
pass
class Cyborg(Robot):
""" implement this class below with appropriate function descriptors
"""
def __init__(self, name, mood, safety_word. pos):
Robot.__init__(self, safety_word, pos)
self.name = name
self.mood = mood
def emote(self):
pass
def announce_move(self, pos_old, pos_new):
pass
def move(self, vector):
pass
1. Robot Class Finish implementing the Robot Class in the provided Python file. Variables and function descriptions have been provided below. Variables str: safety_word The phrase our robot uses to indicate danger float: pos_x The x coordinate of our robot's current position float: pos_y The y coordinate of our robot's current position Functions beep0) Prints "beep" to screen raise_alarm(self, n) Prints the robot's safety word n times to screen with a 1s pause between each print emote(self) Does nothing since robots generally can't emote move(self, vector) Moves the robot by the distances specified by the input vector (a tuple) e.g. if original pos is (4, 5) and move(1,-2)) is called, our robot's new pos is (5, 3)Explanation / Answer
from time import sleep class Robot: """ implement this class below with appropriate function descriptors """ def __init__(self, safety_word, pos): self.pos_x = pos[0] self.pos_y = pos[1] self.safety_word = safety_word def beep(self): """ prints beep :return: """ print("beep") def raise_alarm(self, n): """ prints robot safety_word n times :param n: number of times to print safety word :return: """ for i in range(n): print(self.safety_word + " ") sleep(1) # sleeping delay of 1 second def emote(self): pass def move(self, vector): self.pos_x += vector[0] self.pos_y += vector[1] class Cyborg(Robot): """ implement this class below with appropriate function descriptors """ def __init__(self, name, mood, safety_word, pos): Robot.__init__(self, safety_word, pos) self.name = name self.mood = mood def emote(self): """ shows emotion of robot :return: null """ print("I am " + self.mood) def announce_move(self, pos_old, pos_new): """ announces that it is moving from position to another :param pos_old: previous pos :param pos_new: new pos :return: """ print("{} is moving from position {} to position {}", self.name, pos_old, pos_new) def move(self, vector): """ moves the robot from old pos to new pos and announces the movement :param vector: new pos vector :return: """ old_pos = (super().pos_x, super().pos_y) super().move(vector) new_pos = (super().pos_x, super().pos_y) self.announce_move(old_pos, new_pos)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.