Python Forum

Full Version: Distance between two points!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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)
what units? latitude and longitude?
Yes, latitude and longitude.
(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.
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
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!
(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
(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
(Apr-28-2017, 07:00 AM)Turry Wrote: [ -> ]Either use math.sqrt or **1/2
math.hypot imo.
(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.
Pages: 1 2