Table of Contents
Python Exercise 19 Problem:
In this Python exercise, write a Python program that will take the input from a user to move a robot around a room that will start from the original point of (0,0). The robot has the ability to move FORWARD, BACK, LEFT and RIGHT.
The first input would be the located of the steps (forward, back, left and right). The second part of the input would be the numerical number of steps ( 1, 2, 3, 4, 5 and so on).
The output should compute the current position distance from the sequence of movements and the original location point.
For example, if the user input’s the below, the output should show:
- Forward 6
- Back 2
- Left 1
- Right 7
- Forward 3
The output would be 9.
Solution Python Exercise 19
Python Code Input:
import math pos = [0,0] print("""Move the robot... Please provide the path location and numerical amount of steps for the robot. If you are in the location you like, press enter. """) while True: ds = input("What direction and how many steps? ") if not ds: break path = ds.split(" ") direction = path[0] steps = int(path[1]) direction = direction.upper() if direction=="FORWARD": pos[0]+=steps elif direction=="BACK": pos[0]-=steps elif direction=="LEFT": pos[1]-=steps elif direction=="RIGHT": pos[1]+=steps else: pass print(int(round(math.sqrt(pos[1]**2+pos[0]**2))))
User Input:
What direction and how many steps? forward 9 What direction and how many steps? back 11 What direction and how many steps? left 7 What direction and how many steps? right 9 What direction and how many steps? forward 1 What direction and how many steps? back 5 What direction and how many steps?
Output:
6
The sqrt() is a inbuilt Python function that returns the square root of any number.