1

So I needed to create a script in Python that allows me to calculate distance from one point to another using coordinates. The first point had fixed coordinates and the coordinates to the second point should be through user input. I managed to create that script. The last thing I have to do is to create a statement that allows me to intercept incorrect entries (e.g., letters instead of numbers, nonsensical Degree inputs) using e.g. try...except. So I'm having a bit trouble here since I'm only getting started with Python. Here's my code:

lat1= 47.79
lon1=13.00
name= "Entfernung "
typ= "Km"
userEingabe= input("Geben sie lat2 ein:")
userEingabe2= input("Geben sie lon2 ein:")
lat2 = float(userEingabe)
lon2 = float(userEingabe2)
import math
distanz= 6370*math.acos(math.sin(math.radians(lat1))*math.sin(math.radians(lat2))+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.cos(math.radians(lon2)-math.radians(lon1)))
print(name+str(distanz)+typ)
input("Press return to close this window...")

How can I do this?

2 Answers 2

2

If you want to make sure you got a numeric input you can try to use the following try/except clause:

try:
    lat2 = float(userEingabe)
    lon2 = float(userEingabe2)
except ValueError:
    # alert user, terminate script, etc...

Note that float() will raise a ValueError if the input is non-numerical.

For nonsensical degree inputs you can check if your values are between a specific range or not:

try:
    assert -90 <= lat2 <= 90
    assert -180 <= lon2 <= 180
except AssertionError:
    # alert user, terminate script, etc...

You could modify your code like this so the program keeps asking for lat and lon values if they are not numerical or they don't lie between the defined range

import math

lat1 = 47.79
lon1 = 13.00
name = "Entfernung "
typ = "Km"

# ask for valid input
while True:
    userEingabe = input("Geben sie lat2 ein:")
    userEingabe2 = input("Geben sie lon2 ein:")
    try:
        lat2 = float(userEingabe)
        lon2 = float(userEingabe2)
    except ValueError:
        print('Values have to be numeric')
        continue
    try:
        assert -90 <= lat2 <= 90
        assert -180 <= lon2 <= 180
    except AssertionError:
        print('Values have to make sense')
        continue
    break

# calculate and print distance
distanz = 6370*math.acos(math.sin(math.radians(lat1))*math.sin(math.radians(lat2))+math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.cos(math.radians(lon2)-math.radians(lon1)))
print(name + str(distanz) + typ)
input("Press return to close this window...")
3
  • Oh thanks, could you write me the whole code perhaps, because I'm having errors when i type it in my original one. Just so I could get the sense of it.
    – OSL
    Nov 29, 2019 at 15:48
  • @sqlisql Sure, I edited my answer. I think it is a good starting point and it gives you a good idea of how to accomplish what you want. Nov 29, 2019 at 15:59
  • This is so great! Thank you!
    – OSL
    Nov 29, 2019 at 16:01
0

Looks like a homework/assignment task. Instead of using this forum and other peoples time to solve your problem, maybe you can ask your teacher for help? I am sure he would be so kind...

Not the answer you're looking for? Browse other questions tagged or ask your own question.