Python Forum
Distance between two points!
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Distance between two points!
#1
Help! I'm a total newbie when it comes to programming, I need my program to calculate the distance between two points. I got the first part of my assignment done, I created a function with the distance formula. Here is the second part, please... help :'(. I've been looking on the web for hours. 


Create another function named main that will be responsible for prompting the user for input and calling the distance function. Use exception handling to convert the user input to float. The user input should then be passed as arguments to distance with a function call. Store the return value from the distance function into a variable and then print the results in a pleasing format.


import math

def distance(x1,y1,x2,y2):
     dist = ((x1 - x2)**2 + (y1-y2)**2)**.5 
     return dist

def main(dist):
     try:
          x1 = float(input("Enter a Number: "))
          x2 = float(input("Enter a Second Number: "))
          y1 = float(input("Enter a third number: "))
          y2 = float(input("Enter a forth number: "))
     else:
          print(distance)
Reply
#2
what units? latitude and longitude?
Reply
#3
Yes, latitude and longitude.
Reply
#4
(Apr-28-2017, 01:51 AM)zepel Wrote: ......
Use exception handling to convert the user input to float.
That is plain stupid! You don't use exceptions that way.
 
(Apr-28-2017, 01:51 AM)zepel Wrote:
import math

def distance(x1,y1,x2,y2):
     dist = ((x1 - x2)**2 + (y1-y2)**2)**.5 
     return dist

def main(dist):
     try:
          x1 = float(input("Enter a Number: "))
          x2 = float(input("Enter a Second Number: "))
          y1 = float(input("Enter a third number: "))
          y2 = float(input("Enter a forth number: "))
     else:
          print(distance)
Besides unlucky wording of prompt (covered by @Larz60+):
  1. You use try/except wrong (and you don't nee exception handling)
  2. You are not calling your function with arguments - your print function object
  3. Passing points is usually done by tuple of numbers - though it is not the issue here
And stop Googling - you are not at that level yet; RT(F)M - read the (F) manuals, i.e. tutorials.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
If you are dealing with latitude and longitude then you also need to know how far apart the two locations can be. If they are more that a few 10's of km then you may need to use formulae that take the curvature of the Earth into account.
The equations you are using require a flat plane which the Earth approximates only for relatively short distances.
There are a number of other problems with your code andsome have already been mentioned. Had you even tried to run your code?
Susan
Reply
#6
I tried running my code but the page comes up blank, I'm a second week programming student and my instructor is confusing the hell out of everybody in my class. Gahhhhh!!!

Here are the exact instructions..

Write a python program that declares a function named distance. The purpose of the function is to calculate the distance between two points and return the result.

The function should define 4 parameter variables. The first 2 parameters declare the x and y coordinates of the first point, and the second 2 parameters declare the x and y coordinates of the second point. The distance function should not prompt for input and it should not do any printing.

Create another function named main that will be responsible for prompting the user for input and calling the distance function. Use exception handling to convert the user input to float. The user input should then be passed as arguments to distance with a function call. Store the return value from the distance function into a variable and then print the results in a pleasing format.

Search the web for information about the distance formula if you have forgotten it. Don't forget to call main!
Reply
#7
(Apr-28-2017, 02:49 AM)AussieSusan Wrote: If you are dealing with latitude and longitude then you also need to know how far apart the two locations can be. If they are more that a few 10's of km then you may need to use formulae that take the curvature of the Earth into account.
The equations you are using require a flat plane which the Earth approximates only for relatively short distances.
There are a number of other problems with your code andsome have already been mentioned. Had you even tried to run your code?
Susan
Why are you trying to confuse a guy? That is not the way to help - but a good way to annoy people.

Idiotic waste of forum space to show how big is yours  Naughty ?!

Functions are supposed to be called
distance(x1, x2, x3, x4)
main()
are function calls; you did not call main
distance - in your case - just returns function object

Remove stupid  Wall try/else, call main, fix call to distance - and your code should work

BTW, why are you importing math? You don't need it
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
(Apr-28-2017, 02:52 AM)zepel Wrote: Use exception handling to convert the user input to float.

This is the reason for the try/except
As for the problem itself.
You should make main function to take no arguments *since you get the coordonates from it*.
Inside main , put
print(distance(x1,y1,x2,y2))
Also , try to use only 1 method.
Either use math.sqrt or **1/2
Reply
#9
(Apr-28-2017, 07:00 AM)Turry Wrote: Either use math.sqrt or **1/2
math.hypot imo.
Reply
#10
(Apr-28-2017, 07:28 AM)Mekire Wrote:
(Apr-28-2017, 07:00 AM)Turry Wrote: Either use math.sqrt or **1/2
math.hypot imo.

Yeah. That might be better but I don't think his teacher wants him to use only 1 function to solve the problem.
The idea would be that he "implements" the formula.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Distance Between Points Calculator Spade 1 1,751 Sep-17-2021, 03:14 PM
Last Post: DeaD_EyE
  Distance between indicies of a list johnissa 2 3,040 Apr-25-2018, 01:04 AM
Last Post: johnissa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020