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.
Using Python write the following script as well as using comments By considering the details below,...
Using Python write the following script as well as using comments By considering the details below, write a class that will work out the body mass index for specific values of weight and height. The design of the actual class is shown below: bmi -weight:float -height:float -bmi:float +set_weight(weight:float) +set_height(height:float) +calc_bmi() +get_bmi():float specific designs of the methods are shown below: set_weight set the weight attribute to the value in the parameter weight set_height set the height attribute to the value in...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added...
Please Complete this C Code using the gcc compiler. Please include comments to explain each added line. /*This program computes the Intersection over Union of two rectangles as a percent: IoU = [Area(Intersection of R1 and R2) * 100 ] / [Area(R1) + Area(R2) - Area(Intersection of R1 and R2)] The answer will be specified as a percent: a number between 0 and 100. For example, if the rectangles do not overlap, IoU = 0%. If they are at the...
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)
Write and Compile a python script to solve the 4-queens problem using. The code should allow...
Write and Compile a python script to solve the 4-queens problem using. The code should allow for random starting, and for placed starting using numpy "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 Arc consistency
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.
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t...
Explain the following code using comments next to the code: void foo() { uint8_t a=2; uint8_t b[]={b0, b1, b2}; // They are the last three digits of your A# uint8_t* c=b; uint8_t* d=&a;
It is a strict requirement that this code is implemented in python. The code must be...
It is a strict requirement that this code is implemented in python. The code must be implemented from scratch not using e.g. numpy etc. a. Create a function that takes a matrix X of all sizea as input and gives the transposed matrix as output. Example transposeMatrix([[1,2] ,[3,4] ,[5,6]]) = [[1,3,5] ,[2,4,6]] b. Create a function that multilpies a matrix X1 and X2 of all sizes. Example x1 = [[0,1] ,[1,0] ] x2 = [[1, 0] ,[0,-1] ] matrtixMultl(x1,x2) =...
*** Using Python *** Your program must prompt the user for their city or zip code...
*** Using Python *** Your program must prompt the user for their city or zip code and request weather forecast data from openweathermap.org. Your program must display the weather information in an READABLE format to the user. Requirements: Create a Python Application which asks the user for their zip code or city. Use the zip code or city name in order to obtain weather forecast data from: http://openweathermap.org/ Display the weather forecast in a readable format to the user. Use...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT