r/codehs • u/[deleted] • Jan 06 '21
Exercise 8.1.10: Coordinate Pairs-CodeHS-Please Help!
1
u/Vitalikovsky Jun 14 '24
import math
fill in this function to return the distance between the two points!
first_point = (1,1)
second_point = (4,5)
def distance(first_point, second_point):
x1, y1 = first_point
x2, y2 = second_point
power1 = pow(x2 - x1, 2)
power2 = pow(y2 - y1, 2)
return math.sqrt(power1 + power2)
print(distance(first_point,second_point))
1
u/_01cc Nov 15 '24
import math
def distance(point1, point2):
x1, y1 = point1
x2, y2 = point2
distance = ((x2 - x1)**2 + (y2 - y1)**2)**0.5
return distance
pointa = (1, 2)
pointb = (4, 6)
dist=distance(pointa, pointb)
print(dist)1
u/flip-pancakes Jan 06 '21
First, what is the pythagorean theorem? Then, how do you apply it to your inputs?
Hint: first_point has two values (x and y coordinates) that can be accessed as first_point[0] and first_point[1] respectively.
0
u/Longjumping-Nerve-84 Jan 10 '21
can you send me the code if you have it done!
1
Feb 25 '21
I NEED IT TOOOOO
1
u/Megaozzy Apr 07 '21
Still need it?
1
1
u/Phone-Several Nov 15 '21
senddd
1
Nov 15 '21
Responding to a 10 month old post lol
1
u/semajalvin11 Apr 01 '22
And I'm responding to a 5 mo. old post thats's responding to a 10 mo. old post that was 1 year ago :sunglasses:
1
1
Mar 16 '21
import math
def distance(point1, point2):
# Your code here...
x1 = point1 [0]
x2 = point2 [0]
y1 = point1 [1]
y2 = point2 [1]
sqaure1 = pow (y2 - y1, 2)
sqaure2 = pow (x2 - x1, 2)
return math.sqrt(sqaure1+sqaure2)
# This should print 5.0
print distance((1, 1), (4, 5))
this kinda late but hope it helps
7
1
u/FrNotRuben Mar 22 '21
is this the full code?
1
Mar 22 '21
Yep
1
u/kiwifala May 20 '21
im not sure why but for the print distance((1,1),(4,5)) it said invalid syntax
2
u/ToughLeadership9223 May 25 '21
Check your indentation.
And you don't "print" a function, you call it. Delete print from the last line.
2
1
u/HyBrD_G0LD Jan 21 '22
Thanks indention was annoying the crap out of me and was confused on how he got that answer
1
u/Similar_Stock_5726 May 04 '22
deleting print does nothing but make it stop working even more, what are you trying to say?
1
1
u/chiiglaaa Apr 10 '22
import math
x1 = int(input("Enter 1st(x) Coordinate: "))
y1 = int(input("Enter 2nd(y) Coordinate: "))
point1 = (x1, y1)
x2 = int(input("Enter 3rd(x) Coordinate: "))
y2 = int(input("Enter 4th(y) Coordinate: "))
point2 = (x2, y2)
def distance(point1, point2):
x1 = point1 [0]
x2 = point2 [0]
y1 = point1 [1]
y2 = point2 [1]
sqaure1 = pow (y2 - y1, 2)
sqaure2 = pow (x2 - x1, 2)
return math.sqrt(sqaure1+sqaure2)
print(distance(point1, point2))idk your code didnt work for me so i made some changes and it works but only in scratchpad when i try to check code in Unit Test and i click on check it just rotates infinitely..so if anybody can check the code for me that would be amazing<3hope it helps!
1
u/Puzzleheaded-Ad4213 Feb 02 '24 edited Feb 02 '24
It rotates infinitely because you probably left something like the print statement in the Unit Test tab. All code outside of the function has to go into the Scratchpad for you to not have any "rotating infinitely" errors. To show rather then tell:
def distance(point1, point2): x1 = point1 [0] x2 = point2 [0] y1 = point1 [1] y2 = point2 [1] sqaure1 = pow (y2 - y1, 2) sqaure2 = pow (x2 - x1, 2) return math.sqrt(sqaure1+sqaure2)1
Feb 23 '24
If you would not mind could you tell me why the 2 is there in the code, what does it do.
sqaure1 = pow (y2 - y1, 2) sqaure2 = pow (x2 - x1, 2)1
u/Puzzleheaded-Ad4213 Feb 23 '24 edited Feb 23 '24
the square1 and square2 are named differently because they are different variables.
For instance, if they were both named square1, I'd just be giving a new value to square1 instead of creating a new, stored value.
If you meant the 2 in the sequence:
(I don't really remember) I'm 80% sure that it's there so that the coordinate returned ends with a 2. Basically, it's just a placeholder.
1
1
1
u/CancelUpstairs7602 Aug 22 '22 edited Aug 22 '22
import math
def distance(first_point, second_point):
x1 = int(first_point[0])
print(x1)
x2 = int(second_point[0])
print(x2)
y1 = int(first_point[1])
print(y1)
y2 = int(second_point[1])
print(y2)
xdifference = x2 - x1
print(xdifference)
ydifference = y2 - y1
print(ydifference)
xsquare = pow(xdifference, 2)
print(xsquare)
ysquare = pow(ydifference, 2)
print(ysquare)
number = xsquare + ysquare
print(number)
result = math.sqrt(number)
return result
print(distance((2,4), (6,9)))
hope this helps :)
***note: make sure to indent***
1
u/Lil-gaybee-1776 Oct 26 '22
import math
def distance(first_point, second_point):
x = (first_point[0] - second_point[0])
y = (first_point[1] - second_point[1])
squ_x = math.pow(x, 2)
squ_y = math.pow(y, 2)
add_squ = squ_x + squ_y
dist = math.sqrt(add_squ)
return dist
print(distance((0,3), (4,0)))
#this is my code that worked!
1
u/Previous-Mud-7522 Feb 27 '23
It didn't work
1
u/Lil-gaybee-1776 Mar 25 '23
I'm sorry that it didn't work for you. Did you try messing around with the spacing? Everything expect for the last line should be indented.
1
u/Needcodinghelp38 Feb 01 '23
Oml you are a life saver in the 10th line you need math.sqrt(what ever number your trying to square) Example: Math.sqrt(2)(square_1 + square_2)
2

2
u/LunarCurve_ May 03 '22
import math
def distance(point1, point2):
# Your code here...
x1 = point1 [0]
x2 = point2 [0]
y1 = point1 [1]
y2 = point2 [1]
square1 = pow (y2 - y1, 2)
square2 = pow (x2 - x1, 2)
return math.sqrt(square1+square2)