Getting error on input []: findDistance() takes 0 positional arguments but 1 was given












1















The first part of my code is calculating the distance between two locations on Earth. The second part of my code is returning a list containing all of the cities stored in the database that are in the United States. The database is saved in a file named "city_data.db" containing a table named "cities" with columns ("country_code", "city_name", "region", "population", "latitude", "longitude"). Note that the country code for the United States is "us".



The third part of my code is returning a list containing all of the cities stored in the database that are in the United States sorted by their great circle distance from the city of Buffalo. Use (42.8864, -78.8784) for the latitude/longitude of Buffalo



I am getting error on input : findDistance() takes 0 positional arguments but 1 was given. What am I doing wrong?



import sqlite3
import math
conn = sqlite3.connect("city_data.db")
c = conn.cursor()
conn.commit()

#Part 1
def great_circle_distance(lat1, lon1, lat2, lon2):
radius = 6371

dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1))
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c

return d

#Part 2
def get_us_cities():
get_city = c.execute("SELECT * FROM cities WHERE country_code = 'us'").fetchall()
return [x for x in get_city]

#Part 3
def findDistance():
citieslist = get_us_cities()
lat1 = 42.8864
lon1 = -78.8784
lat2 = citieslist[4]
lon2= citieslist[5]
distance = great_circle_distance(lat1, lon1, lat2, lon2)
return distance

def closest_cities():
array = get_us_cities()
array.sort(key = findDistance)
return array









share|improve this question





























    1















    The first part of my code is calculating the distance between two locations on Earth. The second part of my code is returning a list containing all of the cities stored in the database that are in the United States. The database is saved in a file named "city_data.db" containing a table named "cities" with columns ("country_code", "city_name", "region", "population", "latitude", "longitude"). Note that the country code for the United States is "us".



    The third part of my code is returning a list containing all of the cities stored in the database that are in the United States sorted by their great circle distance from the city of Buffalo. Use (42.8864, -78.8784) for the latitude/longitude of Buffalo



    I am getting error on input : findDistance() takes 0 positional arguments but 1 was given. What am I doing wrong?



    import sqlite3
    import math
    conn = sqlite3.connect("city_data.db")
    c = conn.cursor()
    conn.commit()

    #Part 1
    def great_circle_distance(lat1, lon1, lat2, lon2):
    radius = 6371

    dlat = math.radians(lat2-lat1)
    dlon = math.radians(lon2-lon1)
    a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1))
    * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    d = radius * c

    return d

    #Part 2
    def get_us_cities():
    get_city = c.execute("SELECT * FROM cities WHERE country_code = 'us'").fetchall()
    return [x for x in get_city]

    #Part 3
    def findDistance():
    citieslist = get_us_cities()
    lat1 = 42.8864
    lon1 = -78.8784
    lat2 = citieslist[4]
    lon2= citieslist[5]
    distance = great_circle_distance(lat1, lon1, lat2, lon2)
    return distance

    def closest_cities():
    array = get_us_cities()
    array.sort(key = findDistance)
    return array









    share|improve this question



























      1












      1








      1








      The first part of my code is calculating the distance between two locations on Earth. The second part of my code is returning a list containing all of the cities stored in the database that are in the United States. The database is saved in a file named "city_data.db" containing a table named "cities" with columns ("country_code", "city_name", "region", "population", "latitude", "longitude"). Note that the country code for the United States is "us".



      The third part of my code is returning a list containing all of the cities stored in the database that are in the United States sorted by their great circle distance from the city of Buffalo. Use (42.8864, -78.8784) for the latitude/longitude of Buffalo



      I am getting error on input : findDistance() takes 0 positional arguments but 1 was given. What am I doing wrong?



      import sqlite3
      import math
      conn = sqlite3.connect("city_data.db")
      c = conn.cursor()
      conn.commit()

      #Part 1
      def great_circle_distance(lat1, lon1, lat2, lon2):
      radius = 6371

      dlat = math.radians(lat2-lat1)
      dlon = math.radians(lon2-lon1)
      a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1))
      * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
      c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
      d = radius * c

      return d

      #Part 2
      def get_us_cities():
      get_city = c.execute("SELECT * FROM cities WHERE country_code = 'us'").fetchall()
      return [x for x in get_city]

      #Part 3
      def findDistance():
      citieslist = get_us_cities()
      lat1 = 42.8864
      lon1 = -78.8784
      lat2 = citieslist[4]
      lon2= citieslist[5]
      distance = great_circle_distance(lat1, lon1, lat2, lon2)
      return distance

      def closest_cities():
      array = get_us_cities()
      array.sort(key = findDistance)
      return array









      share|improve this question
















      The first part of my code is calculating the distance between two locations on Earth. The second part of my code is returning a list containing all of the cities stored in the database that are in the United States. The database is saved in a file named "city_data.db" containing a table named "cities" with columns ("country_code", "city_name", "region", "population", "latitude", "longitude"). Note that the country code for the United States is "us".



      The third part of my code is returning a list containing all of the cities stored in the database that are in the United States sorted by their great circle distance from the city of Buffalo. Use (42.8864, -78.8784) for the latitude/longitude of Buffalo



      I am getting error on input : findDistance() takes 0 positional arguments but 1 was given. What am I doing wrong?



      import sqlite3
      import math
      conn = sqlite3.connect("city_data.db")
      c = conn.cursor()
      conn.commit()

      #Part 1
      def great_circle_distance(lat1, lon1, lat2, lon2):
      radius = 6371

      dlat = math.radians(lat2-lat1)
      dlon = math.radians(lon2-lon1)
      a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1))
      * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
      c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
      d = radius * c

      return d

      #Part 2
      def get_us_cities():
      get_city = c.execute("SELECT * FROM cities WHERE country_code = 'us'").fetchall()
      return [x for x in get_city]

      #Part 3
      def findDistance():
      citieslist = get_us_cities()
      lat1 = 42.8864
      lon1 = -78.8784
      lat2 = citieslist[4]
      lon2= citieslist[5]
      distance = great_circle_distance(lat1, lon1, lat2, lon2)
      return distance

      def closest_cities():
      array = get_us_cities()
      array.sort(key = findDistance)
      return array






      python sql database algorithm sorting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 4:27

























      asked Nov 25 '18 at 4:14







      user10649535































          1 Answer
          1






          active

          oldest

          votes


















          0














          You're doing



          array.sort(key = findDistance)


          That means that the sorting algorithm is using findDistance() to find a "key" for each value it's sorting. Basically, it takes a list, does findDistance() for every item in that list, and then sorts the list according to the return values. As such, array.sort() expects that whatever it's given as a key will be able to take one value and spit out another value that can be used for sorting.



          I don't know how you're structuring your database, or what format your array is going to be in, but let's assume for this example that each item in the array is a list, with indexes corresponding to the columns you listed (for example, "latitude" would be index 4, "longitude would be index 5, etc). Then, you'd want to rewrite findDistance() to take one of those as a parameter:



          def findDistance(city):
          lat1 = 42.8864
          lon1 = -78.8784
          lat2 = city[4] # notice how we're using the variable that was passed in,
          lon2 = city[5] # instead of accessing get_us_cities()
          distance = great_circle_distance(lat1, lon1, lat2, lon2)
          return distance





          share|improve this answer
























          • Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

            – user10649535
            Nov 25 '18 at 4:55








          • 1





            Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

            – Green Cloak Guy
            Nov 25 '18 at 5:01











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464603%2fgetting-error-on-input-finddistance-takes-0-positional-arguments-but-1-was%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown
























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You're doing



          array.sort(key = findDistance)


          That means that the sorting algorithm is using findDistance() to find a "key" for each value it's sorting. Basically, it takes a list, does findDistance() for every item in that list, and then sorts the list according to the return values. As such, array.sort() expects that whatever it's given as a key will be able to take one value and spit out another value that can be used for sorting.



          I don't know how you're structuring your database, or what format your array is going to be in, but let's assume for this example that each item in the array is a list, with indexes corresponding to the columns you listed (for example, "latitude" would be index 4, "longitude would be index 5, etc). Then, you'd want to rewrite findDistance() to take one of those as a parameter:



          def findDistance(city):
          lat1 = 42.8864
          lon1 = -78.8784
          lat2 = city[4] # notice how we're using the variable that was passed in,
          lon2 = city[5] # instead of accessing get_us_cities()
          distance = great_circle_distance(lat1, lon1, lat2, lon2)
          return distance





          share|improve this answer
























          • Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

            – user10649535
            Nov 25 '18 at 4:55








          • 1





            Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

            – Green Cloak Guy
            Nov 25 '18 at 5:01
















          0














          You're doing



          array.sort(key = findDistance)


          That means that the sorting algorithm is using findDistance() to find a "key" for each value it's sorting. Basically, it takes a list, does findDistance() for every item in that list, and then sorts the list according to the return values. As such, array.sort() expects that whatever it's given as a key will be able to take one value and spit out another value that can be used for sorting.



          I don't know how you're structuring your database, or what format your array is going to be in, but let's assume for this example that each item in the array is a list, with indexes corresponding to the columns you listed (for example, "latitude" would be index 4, "longitude would be index 5, etc). Then, you'd want to rewrite findDistance() to take one of those as a parameter:



          def findDistance(city):
          lat1 = 42.8864
          lon1 = -78.8784
          lat2 = city[4] # notice how we're using the variable that was passed in,
          lon2 = city[5] # instead of accessing get_us_cities()
          distance = great_circle_distance(lat1, lon1, lat2, lon2)
          return distance





          share|improve this answer
























          • Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

            – user10649535
            Nov 25 '18 at 4:55








          • 1





            Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

            – Green Cloak Guy
            Nov 25 '18 at 5:01














          0












          0








          0







          You're doing



          array.sort(key = findDistance)


          That means that the sorting algorithm is using findDistance() to find a "key" for each value it's sorting. Basically, it takes a list, does findDistance() for every item in that list, and then sorts the list according to the return values. As such, array.sort() expects that whatever it's given as a key will be able to take one value and spit out another value that can be used for sorting.



          I don't know how you're structuring your database, or what format your array is going to be in, but let's assume for this example that each item in the array is a list, with indexes corresponding to the columns you listed (for example, "latitude" would be index 4, "longitude would be index 5, etc). Then, you'd want to rewrite findDistance() to take one of those as a parameter:



          def findDistance(city):
          lat1 = 42.8864
          lon1 = -78.8784
          lat2 = city[4] # notice how we're using the variable that was passed in,
          lon2 = city[5] # instead of accessing get_us_cities()
          distance = great_circle_distance(lat1, lon1, lat2, lon2)
          return distance





          share|improve this answer













          You're doing



          array.sort(key = findDistance)


          That means that the sorting algorithm is using findDistance() to find a "key" for each value it's sorting. Basically, it takes a list, does findDistance() for every item in that list, and then sorts the list according to the return values. As such, array.sort() expects that whatever it's given as a key will be able to take one value and spit out another value that can be used for sorting.



          I don't know how you're structuring your database, or what format your array is going to be in, but let's assume for this example that each item in the array is a list, with indexes corresponding to the columns you listed (for example, "latitude" would be index 4, "longitude would be index 5, etc). Then, you'd want to rewrite findDistance() to take one of those as a parameter:



          def findDistance(city):
          lat1 = 42.8864
          lon1 = -78.8784
          lat2 = city[4] # notice how we're using the variable that was passed in,
          lon2 = city[5] # instead of accessing get_us_cities()
          distance = great_circle_distance(lat1, lon1, lat2, lon2)
          return distance






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 4:40









          Green Cloak GuyGreen Cloak Guy

          2,5031720




          2,5031720













          • Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

            – user10649535
            Nov 25 '18 at 4:55








          • 1





            Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

            – Green Cloak Guy
            Nov 25 '18 at 5:01



















          • Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

            – user10649535
            Nov 25 '18 at 4:55








          • 1





            Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

            – Green Cloak Guy
            Nov 25 '18 at 5:01

















          Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

          – user10649535
          Nov 25 '18 at 4:55







          Can you explain a little bit more of why I want to pass city as a parameter to finddistance() instead of trying to access latitude and longitude from get_us_cities(). Because somehow, your code gives me the correct output.

          – user10649535
          Nov 25 '18 at 4:55






          1




          1





          Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

          – Green Cloak Guy
          Nov 25 '18 at 5:01





          Don't forget, get_us_cities() gives you the entire list of cities (or a list of lists, in this case) - when you call array.sort(), you're doing it on an array that you already initialized to that list of cities. The sort() function will automatically apply the key to each element of that list, by passing that element into findDistance(). So, get_us_cities() is already involved by the time findDistance() is called - you don't have to bring it in again.

          – Green Cloak Guy
          Nov 25 '18 at 5:01


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464603%2fgetting-error-on-input-finddistance-takes-0-positional-arguments-but-1-was%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)