I need your help on my Python programming course with the function for the hypotenuse. I've included the guide on what I need for 6,8,10 hypotenuse. These are the integers I chose myself to make a new function for my hypotenuse. I need it for the function instructed in the assignment, please. The example hypotenuse function as follows; def distance(x1,y1,x2,y2): return 0.0 to the test new function, call it with sample arguments: >>>distance(1,2,4,6) 0.0 I would like to choose the horizontal distance at 6 and the vertical distance at 8 that way the result is 10, the hypotenuse of a 6-8-10 triangle. At this point we have confirmed that the function is syntactically correct, and we can start adding code to the body. A reasonable next step is to find the differences x2 -x1 and y2 -y1. The next version stores those values in temporary variables and prints them. def distance(x1, y1, x2,y2): dx = x2-x1 dy = y2-y1 print('dx is',dx) print('dy is',dy) return 0.0 If the function is working, it should display dx is 6 and dy is 8. If so, we know that the function is getting the right arguments and performing the first computation correctly. If not, there are only a few lines to check. Next we compute the sum of squares of dx and dy: def distance(x1, y1,x2,y2): dx = x2-x1 dy = y2-y1 dsquared = dx**2 + dy**2 print('dsquared is ', dsquared) return 0.0 Again, you would run the program at this sage and check the output (which should be 10). Finally, you can use math.sqrt to compute and return the result: def distance(x1,y1,x2,y2): dx = x2 - x1 dy = y2-y1 dsquared = dx**2 + dy**2 result = math.sqrt(dsquared) return result if that works correctly, you are done. Otherwise, you might want to print the value of result before the return statement. The final version of the function doesn't display anything when it runs; it only returns a value. The prints statements we wrote are useful for debugging, but once you get the function working, you should remove them.
I need your help on my Python programming course with the function for the hypotenuse. I've included the guide on what I need for 6,8,10 hypotenuse. These are the integers I chose myself to make a new function for my hypotenuse. I need it for the function instructed in the assignment, please.
The example hypotenuse function as follows;
def distance(x1,y1,x2,y2):
return 0.0
to the test new function, call it with sample arguments:
>>>distance(1,2,4,6)
0.0
I would like to choose the horizontal distance at 6 and the vertical distance at 8 that way the result is 10, the hypotenuse of a 6-8-10 triangle.
At this point we have confirmed that the function is syntactically correct, and we can start adding code to the body. A reasonable next step is to find the differences x2 -x1 and y2 -y1. The next version stores those values in temporary variables and prints them.
def distance(x1, y1, x2,y2):
dx = x2-x1
dy = y2-y1
print('dx is',dx)
print('dy is',dy)
return 0.0
If the function is working, it should display dx is 6 and dy is 8. If so, we know that the function is getting the right arguments and performing the first computation correctly. If not, there are only a few lines to check.
Next we compute the sum of squares of dx and dy:
def distance(x1, y1,x2,y2):
dx = x2-x1
dy = y2-y1
dsquared = dx**2 + dy**2
print('dsquared is ', dsquared)
return 0.0
Again, you would run the program at this sage and check the output (which should be 10). Finally, you can use math.sqrt to compute and return the result:
def distance(x1,y1,x2,y2):
dx = x2 - x1
dy = y2-y1
dsquared = dx**2 + dy**2
result = math.sqrt(dsquared)
return result
if that works correctly, you are done. Otherwise, you might want to print the value of result before the return statement.
The final version of the function doesn't display anything when it runs; it only returns a value. The prints statements we wrote are useful for debugging, but once you get the function working, you should remove them.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images