0

Hey I'm new to python and working on a uni assignment and need help. So I have a query of cities and some properties to go with them and i need to print out a list of the bordering cities and the cities more south and west of the city of Guelph. Apparently it can be done in 3 lines but I just want to figure out how to get my code to work for now.

ontario = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Guelph",
                "border town?": "false"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-80.255126953125, 43.545561990655855]
            }
        }
    ]
}

citiesInOntario = ontario["features"]

for city in citiesInOntario:
    if(city["properties"]["border town?"] == "true" and \
       city["geometry"]["coordinates"][0] < -80.255 and \
       city["geometry"]["coordinates"] < 43.545):
      print(city["properties"]["name"])
SteveK
  • 995
  • 5
  • 17
  • 3
    What does this code do? And what do you want it to do differently? – Code-Apprentice Apr 14 '20 at 19:31
  • 4
    You do `city["geometry"]["coordinates"][0] < -80.255` and then `city["geometry"]["coordinates"] < 43.545`. The first one is correct but the second is not. Can you see the difference? – Code-Apprentice Apr 14 '20 at 19:33
  • 2
    BTW, you shouldn't use strings for true/false, use booleans. – Barmar Apr 14 '20 at 19:33
  • Theres a list of 15 other cities with guelph and i need it to print out the cities that are a board town so = true and are more south and west of guelphs coordinates so < then guelphs coords. Im just getting errors with mine now – Thomas Van Alphen Apr 14 '20 at 19:37
  • Oh i was just using a few examples my prof gave us to help and lead us in the correct direction and that is basically what he did but i am VERY new to python so im alittle confused hehe – Thomas Van Alphen Apr 14 '20 at 19:39
  • @ThomasVanAlphen The point @Barmar is making is that you should use Python's `True` and `False` instead of the string "true" and "false". The line `"border town?": "false"` should become `"border town?": False`. That would allow your later checks to go from `if(city["properties"]["border town?"] == "true" and ...` to `if(city["properties"]["border town?"] and ...` – SteveK Apr 14 '20 at 19:40
  • 1
    Please [edit] your post and add what's wrong with it. See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) in the [Help]. – Jongware Apr 14 '20 at 19:42
  • 1
    You just need to add `[1]` to the second `city['geometry']['coordinates']` – Barmar Apr 14 '20 at 19:43
  • okay i think i get that Steve ill try it out. Can i ask whats wrong with using the string? Does python not like strings? Sorry im super new as this is a make up lab that my prof threw together so we could finish the year! – Thomas Van Alphen Apr 14 '20 at 19:44
  • @ThomasVanAlphen See the explanation given in the answer https://stackoverflow.com/questions/715417/converting-from-a-string-to-boolean-in-python – SteveK Apr 14 '20 at 19:45
  • @ barmar you edited it and added / behind the and's what does the / do? – Thomas Van Alphen Apr 14 '20 at 19:46
  • @ThomasVanAlphen It prevents you from having incredibly long lines of code (which aren't always easy to read) https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python – SteveK Apr 14 '20 at 19:48
  • Thanks steve! ill check those out – Thomas Van Alphen Apr 14 '20 at 19:48
  • Thanks guys or gals for the help got the code to print out the two cities i needed! Even though i dont know why it works vs before but hey it works! Stay safe out there and thanks again Steve and Barmar – Thomas Van Alphen Apr 14 '20 at 19:52

1 Answers1

1

As people have pointed out in the comments, your conditions for the coordinates don't match

       city["geometry"]["coordinates"][0] < -80.255 and \
       city["geometry"]["coordinates"] < 43.545):

In the first one you're getting the first coordinate value from the list city["geometry"]["coordinates"] and comparing it, but in the second condition you're comparing the entire list to a number. You just need to reference the second element of the list with city["geometry"]["coordinates"][1].

       city["geometry"]["coordinates"][0] < -80.255 and \
       city["geometry"]["coordinates"][1] < 43.545)

Also consider changing your "true" and "false" strings to Python's True and False:

ontario = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "name": "Guelph",
                "borders_town": False
            },
            "geometry": {
                "type": "Point",
                "coordinates": [-80.255126953125, 43.545561990655855]
            }
        }
    ]
}

so that your new if condition is now:

    if(city["properties"]["borders_town"] and ...
SteveK
  • 995
  • 5
  • 17