Help with airline project

Hello i am currently trying to complete a project with a string of airports full with longitude and latitude where a user would be able to get either the minimum distance of said airports or if he the distance between 2 of them if he so chooses to.I am going to complete this using haversine formula. but for now i am stuck in setting my function for the minimum distance and the function if 2 airports are chosen from the list.I dont know it id get past it but for now traceback sais radians is string so nada.

First of all thanks for any and all help!
I would appreciate it if u could offer hints instead of complete code parts :stuck_out_tongue:

Blockquote
import math

airports =
airport_info = “”"
country1 40.855869°N 25.956264°E
c2 37.936389°N 23.947222°E
c3 35.531667°N 24.149722°E
c4 38.343056°N 26.140556°E
c5 39.601944°N 19.911667°E
c6 35.339722°N 25.180278°E
c7 37.068333°N 22.025556°E
c8 40.913333°N 24.619167°E
c9 38.12°N 20.500278°E
c10 36.793336°N 27.091667°E
c11 39.917072°N 25.236308°E
c12 39.0567°N 26.5994°E
c13 37.020833°N 25.113056°E
c14 36.405419°N 28.086192°E
15 37.6891°N 26.9116°E
16 40.519722°N 22.970833°E
c17 37.750833°N 20.884167°E"""

part = airport_info.split()
airports = list(zip(part[1: : 3], part[2: : 3]))
airports2 = airports[0: ]

def distance():

for i in range(len(airports) - 1):
    
    lon1 = radians(airports[i][1])
    lat1 = radians(airports[i][2])
    
    for j  in range(len(airports2) - 1):
        
        lon2 = radians(airports[i+1][1])
        lat2 = radians(airports[i+1][2])
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        distance = R * c
        distance_list = []
        distance_list.append(distance)
        
        return min(sorted.distance_list)

Blockquote

You likely need to convert strings to floating point numbers by using float as in radians(float(airports[...][...])).

[André Roberge] thanks for getting back,but traceback is cannot convert string to float, because of the degrees and bearing i think,how do i get rid of those while being in subtuple list or should i split it differently?

We all very much appreciate that too :grinning_face_with_smiling_eyes:

I believe you are correct. To strip them, you can add one more layer of slicing with [:-2] to strip the last two characters (passing negative integers to a slice counts from the end rather than the beginning). You could also use replace, strip, etc. in different ways, but assuming you always know they will be exactly two characters, using slicing is the simplest solution.

Best of luck!

Ty sir ive seen the light(in my inexperienced mind elements of the list in question where still ‘string’ enough to cause me problems but not so much as to consider them a sum of characters which i could easily remove one by one with slice :slight_smile: )
One last question if i may,
i know how i can add functions in a dict and call upon them based on user input,but will that work if user input is for e.x the word ‘min’ which is a build in func in Python(which u didnt need me to tell you :stuck_out_tongue: )

Actually, responding to another user’s post today I had to remind myself whether sum is a builtin, since I’m so used to working with Numpy arrays and Pandas dfs :laughing:

Dict keys are just (hashable) Python objects, not names (often but not always strings), and values can be any Python object, including builtin functions (which, like everything in Python, are also objects). So you can just do something like:

FUNCS = {"min": min, "max": max}
func_name = input("Type the name of a function to call:")
print(FUNCS[func_name]([1, 2, 3]))

If you know the function the user will be passing is a builtin and the user is just passing the literal name, you can do:

import builtins
func_name = input("Type the name of a function to call:")
print(getattr(builtins, func_name)())

But you probably shouldn’t, since the user might call a function that doesn’t make sense with the provided args, does something unexpected or potentially even harms your system, so better to explictly define the functions you want them to use in a dict. Similarly, you can build up your call as a string and then eval() it, but that is very unsafe and prone to errors and security risks and should never be used outside of very specific situations where you can’t avoid it.

Thanks for responding,i solved it on my own(im exstatic),problem was i wanted user to have 3 options just to make my life miserable,1st : null input(enter key)2nd: the word ‘min’ which is the build in function i wanted help with i couldnt parse it cause i didnt know when checking for it i could use ’ ’ to make it stop being purple nasty build in fucntion word in my if statement and lastly is 2 numbers which will be 2 airports chosen from the list to find the distance between them,id like to add em all in a dict and cook up one hell of a menu() function calling other functions based on input but this pandawan has a lot to learn !!!

print(‘much obliged for all help’)

:smiley: