#Quiz09

Task

Write a function that receives four parameters: x1, y1, x2, y2 which are all floating point values.

The function is called distance and returns (float) the distance between x1,y1 and x2,y2 on the cartesian coordinate plane.

 

Solution:


import math
def dist(x1, y1, x2, y2):
x = x2-x1
y = y2-y1
z = (x**2) + (y**2)
ans= math.sqrt(z)
return ans
#MainProgramBelow
x1 = float(input("Write the x value for the first coordinate"))
y1 = float(input("Write the y value for the first coordinate"))
x2 = float(input("Write the x value for the second coordinate"))
y2 = float(input("Write the y value for the second coordinate"))
print("The distance in between the two coordinates is:", dist(x1, y1, x2, y2))

view raw

Quiz09

hosted with ❤ by GitHub

Solution using tuples:


#Quiz09alternative
import math
def dist(a,b):
x = b[0] -a[0]
y = b[1] – a[1]
z = x**2 + y**2
ans= math.sqrt(z)
return ans
#MainProgramBelow
x1 = float(input("Write the x value for the first coordinate"))
y1 = float(input("Write the y value for the first coordinate"))
a = (x1,y1)
x2 = float(input("Write the x value for the second coordinate"))
y2 = float(input("Write the y value for the second coordinate"))
b = (x2,y2)
print("The distance in between", a ,"and", b , "is:", dist(a,b))

4 thoughts on “#Quiz09

Leave a comment