Question

In: Computer Science

#python. Explain code script of each part using comments for the reader. The code script must...

#python. Explain code script of each part using comments for the reader. The code script must work without any errors or bugs.
Ball moves in a 2D coordinate system beginning from the original point (0,0). The ball can
move upwards, downwards, left and right using x and y coordinates. Code must ask the
user for the destination (x2, y2) coordinate point by using the input x2 and y2 and the known
current location, code can use the euclidean distance formula to work out the distance
to the destination. Code should repeatedly ask the user to input the destination
coordinates to shift the ball further but, once the user types the value 999, the code
should print all previous movements of the ball by showing the distance of each step. Also trace function
prints the total distance travelled by the ball.

Solutions

Expert Solution

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you need any change in code. Else please upvote

CODE:

import math

x_cordinates = [0] #Declare list to store the x cordinates and initialize it with starting x cordinate 0

y_cordinates = [0] #Declare list to store the y cordinates and initialize it with starting y cordinate 0

while True: #Loop continuously

    x = int(input("\nEnter the destination x coordinate to shift the ball: ")) #Reading the x cordinate of destination

    if(x == 999): #If the value entered is 999, break the loop

        break

    y = int(input("Enter the destination y coordinate to shift the ball: ")) #Reading the y cordinate of destination

    if(y == 999): #If the value entered is 999, break the loop

        break

    x_cordinates.append(x) #Adding the x cordinate to list x_cordinates

    y_cordinates.append(y) #Adding the y cordinate to list y_cordinates

print("\nSource   Destination     Distance") #Displaying table header

for i in range(len(x_cordinates) - 1): #Loop through each index in list

    #getting the current x and y cordinates to x1 and y1

    x1 = x_cordinates[i]

    y1 = y_cordinates[i]

    #getting the next x and y cordinates to x2 and y2

    x2 = x_cordinates[i+1]

    y2 = y_cordinates[i+1]

   

    distance = math.sqrt( ((x1-x2)**2)+((y1-y2)**2)) #Calculating the distance between the two points

   

    print("({:d},{:d})    ({:d}, {:d}) {:>15.2f}".format(x1, y1, x2, y2, distance)) #Displaying the cordinates and distance

   

   

OUTPUT:


Related Solutions

Comments using # and "" "should also be included for reader to understand each part. Question:...
Comments using # and "" "should also be included for reader to understand each part. Question: 14. Write in python a script code which tells the user to enter an integer bigger than 5. Any input numbers which are smaller than 5 or 5 are not considered by this script. The code then should output all the prime numbers starting at 5, which are smaller than the number inputted by that user.
Write a python script to solve the 4-queens problem using. The code should allow for random...
Write a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting. "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens can capture each other. That is, no two queens are allowed to be placed on the same row, the same column or the same diagonal." Display the iterations until the final solution Hill Climbing (your choice of variant)
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed initially reflecting the appropriate time of day (for example: Good Night, Welcome to Sussex Bank). b. Assume the user’s account balance is $5375.27. c. Allow the user to enter a pin number that does not have to be validated: def atm_login(): pin_number = input("Please enter your (4 digit) pin number: ") # display welcome message welcome_message() d. The message should be followed by a...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
In this python script find any bugs and errors which would cause the code to crash....
In this python script find any bugs and errors which would cause the code to crash. The code must be re-written correctly. After debugging make a separate list of all the errors you found in the script. contacts_list=[] # global variable, list of contacts_list, one string per contact def pause()     """ pauses program e.g. to view data or message """     input("press enter to continue") def load():     """ populate list with data """          contacts_list.append(('Milo ', '063847489373'))...
Python Explain Code #Python program class TreeNode:
Python Explain Code   #Python program class TreeNode:    def __init__(self, key):        self.key = key        self.left = None        self.right = Nonedef findMaxDifference(root, diff=float('-inf')):    if root is None:        return float('inf'), diff    leftVal, diff = findMaxDifference(root.left, diff)    rightVal, diff = findMaxDifference(root.right, diff)    currentDiff = root.key - min(leftVal, rightVal)    diff = max(diff, currentDiff)     return min(min(leftVal, rightVal), root.key), diff root = TreeNode(6)root.left = TreeNode(3)root.right = TreeNode(8)root.right.left = TreeNode(2)root.right.right = TreeNode(4)root.right.left.left = TreeNode(1)root.right.left.right = TreeNode(7)print(findMaxDifference(root)[1])
Understand the code and explain the code and answer the questions. Type your answers as comments....
Understand the code and explain the code and answer the questions. Type your answers as comments. #include #include using namespace std; // what is Color_Size and why it is at the end? enum Color {        Red, Yellow, Green, Color_Size }; // what is Node *next and why it is there? struct Node {        Color color;        Node *next; }; // explain the code below void addNode(Node* &first, Node* &last, const Color &c) {        if (first == NULL)...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT