Question: Purpose: Modify a class to make it easier to use with overloading. Open the notebook RoboWorld.ipynb. Save a copy in your Drive and call it

Purpose: Modify a class to make it easier to use with overloading.
Open the notebook RoboWorld.ipynb. Save a copy in your Drive and call it Roboworld overload.ipynb.
Add a new
method that returns a string for use with Python's built-in print function. Hint:
it may look very similar to print_state
Rewrite the test code in the second code block to use print instead of the instance method
Run both your class and test code blocks.
Further hint: You should write an
method that returns a string. Then, you can simply say
For an example, see the Time Class notebook, or Section 17.6 in Think Python 2e
Turn in: Use File Download .ipynb to download RoboWorld Overload.ipynb for upload to
Gradescope below, comprising the answer of Problem A.2. Make sure the output from your test
block is present, else re-run before saving.
The file:
# Robot Class
# (c) Michael S. Branicky, 2020-10-28,2021-04-08
class Robot:
# Attributes (of each Robot instance): x, y, dir
# y N
# ^^
# | W > E
# +--> x v
# S
# Class attributes/variables
NORTH =0
EAST =1
SOUTH =2
WEST =3
def __init__(self):
self.x =0
self.y =0
self.dir = Robot.NORTH
def rotate_right(self):
self.dir =(self.dir +1)%4
def rotate_left(self):
self.dir = self.dir -1
if self.dir 0:
self.dir =3
def forward(self):
if self.dir == Robot.NORTH:
self.y +=1
elif self.dir == Robot.SOUTH:
self.y -=1
elif self.dir == Robot.EAST:
self.x +=1
elif self.dir == Robot.WEST:
self.x -=1
def print_state(self):
print_string = f"Robot at ({self.x},{self.y})"
if self.dir == Robot.NORTH:
dir_symbol ="^"
elif self.dir == Robot.SOUTH:
dir_symbol ="v"
elif self.dir == Robot.EAST:
dir_symbol =">"
elif self.dir == Robot.WEST:
dir_symbol =""
print_string += dir_symbol
print(print_string)
# ALTERNATE 1:
# dir_symbols ="^>v"
# dir_symbol = dir_symbols[self.dir]
# ALTERNATE 2:
# dir_symbols ={Robot.NORTH: "^", Robot.SOUTH: "v", Robot.EAST: ">", Robot.WEST: ""}
# dir_symbol = dir_symbols[self.dir]
Purpose: Modify a class to make it easier to use

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!