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...
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.
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;
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
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 The script is to open a given file. The user is to be asked...
Using Python The script is to open a given file. The user is to be asked what the name of the file is. The script will then open the file for processing and when done, close that file. The script will produce an output file based on the name of the input file. The output file will have the same name as the input file except that it will begin with "Analysis-". This file will be opened and closed by...
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 *
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT