Maaike Scholtemeijer
Maaike Scholtemeijer

Reputation: 31

TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

For my course in Python, I am creating a program that calculates the distance between two cities bases on their coordinates. It has worked and suddenly I got the following error:

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    distance(+1, 52, 22, +1, 4, 32, +1, 45, 30, -1, 73, 35)
  File "C:/Python27/flying_distances_1.py", line 26, in distance
    distance = Haversine(lat_1, lat_2, lon_1, lon_2) * 6367.0
  File "C:/Python27/flying_distances_1.py", line 4, in Haversine
    a_1 = math.sin((lat_2 - lat_1)/2) ** 2
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

This is the code that goes with it:

import math

def Haversine (lat_1, lat_2, lon_1, lon_2):
    a_1 = math.sin((lat_2 - lat_1)/2) ** 2
    a_2 = math.cos(lat_1) * math.cos(lat_2)
    a_3 = math.sin((lon_2-lon_1)/2) ** 2
    a_4 = a_1 + a_2 * a_3
    b = 1 - a_4
    d = 2 * math.atan2(math.sqrt(a_4), math.sqrt(b))

def conversion (sign, degrees, minutes):
    minutes_to_degrees = 1/60.0 * minutes
    total_degrees = minutes_to_degrees + degrees
    radians = total_degrees * 1/180.0 * math.pi
    total_radians = sign * radians

def distance (sign_lat_1, deg_lat_1, min_lat_1,
              sign_lon_1, deg_lon_1, min_lon_1,
              sign_lat_2, deg_lat_2, min_lat_2,
              sign_lon_2, deg_lon_2, min_lon_2):
    lat_1 = conversion(sign_lat_1, deg_lat_1, min_lat_1)
    lon_1 = conversion(sign_lon_1, deg_lon_1, min_lon_1)
    lat_2 = conversion(sign_lat_2, deg_lat_2, min_lat_2)
    lon_2 = conversion(sign_lon_2, deg_lon_2, min_lon_2)
    distance = Haversine(lat_1, lat_2, lon_1, lon_2) * 6367.0
    return distance

I have searched and searched but I can't seem to find the error that causes the aforementioned message in my code. It probably is something really small (and possibly quite stupid ;)), but the person that can find the error will help me back on track!

Upvotes: 0

Views: 4696

Answers (1)

James K
James K

Reputation: 3752

There is no return statement in the conversion function. At the moment, the radians value is calculated then forgotten when the function finishes. If you want the value of total_radians to be accessed from outside the function, add

    return total_radians

as the last line of the conversion function.

Upvotes: 2

Related Questions