In: Computer Science
#!/usr/bin/env python3
class Robot:
def_init_(self,instruction_file):
self.instruction_list=None
def write_instructions(self):
f=open(self.instruction_file,'w')
while True:
movement_x=input("enter horizontal movement,or q to quit:")
if movement_x== 'q':
break
movement_y=input('enter vertical movement, or q to quit:' )
if movement_y=='q':
break#store x and y movements alternately on separate lines
#write both x and y movements when both are received
f.write(movement_x+'\n')
f.write(movement_y+'\n')
f.close()
def read_instruction(self):
try:
f=open(self.instruction_file,'r')
print (error)
else:
#store (movements +'\n')as elements in alist
self.instructions_list+f.readlines()
f.close()
def get_location(self):
position_x,position_y=0,0
distance_x,distance_y=0,0
self.read_instructions()
if self.instructions_list !=None:
for data in self.instruction_list:
# exclude'\n' at the end of the movement
temp=data[:-1]
#get x movement from odd line numbers
if self.instructions_list.index(data)%2==0:
position_x +=float (temp)
distance_x +=abs(float temp))
else:
get y movement from even line numbers
position_y +=float (temp)
distance_y +=abs(float temp))
return position_x,position_y, distance_x,distance_y
Robot_1=Robot('robot _1.get_location()
print(' the robot is finally at ' +str(pos_x))
print('the robot has traveled a distance of ' +str(dis_x) +'horizontally, ')
+str(dis_y)+ 'vertically')