4

Printing zip lists whose data comes from input user in a loop I have made a program that calculates area and perimeter based on 2D coordinates (latitude and latitude) given by the user, asking if they want to include more coordinates from the third pair, and evaluating the area and perimeter each time a pair of coordinates are added.

Besides, it compares the values of the area and perimeter from different regions, choosing how many regions the user want to evaluate.

from pyproj import Geod

geod = Geod(ellps='WGS84')

regions = 0
area_list = []
perimeter_list = []
while True:
    regions += 1
    lats = []
    lons = []
    count = 0
    while True:
        count += 1
        lat = float(input(f"Enter latitude #{count}: "))
        lon = float(input(f"Enter longitude #{count}: "))
        lats.append(lat)
        lons.append(lon)
    
        if count < 3:
            print("Not enough information to calculate the area.")
            continue
    
        poly_area, poly_perimeter = geod.polygon_area_perimeter(lons, lats)
        print(f"Área #{regions}:", (abs(float(poly_area))) / (1000000), "km^2")
        print(f"Perímetro #{regions}:", (abs(float(poly_perimeter))) / (1000), "km")
        if input('Enter more coordinates? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
            area_list.append(poly_area)
            perimeter_list.append(poly_perimeter)
#            p1 = Polygon([lat lon])
            break 
    if input('More regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
        break 

while True:
    dividend = int(input(f"Write what region is the dividend:")) - 1
    area_km_dividend = (abs(area_list[dividend])/1000000)
    perimeter_km_dividend = (abs(perimeter_list[dividend])/1000)
    print(f"Area: {area_km_dividend}")
    print(f"Perimeter: {perimeter_km_dividend}")
    
    divisor = int(input(f"Which region is the divisor?:")) - 1
    area_km_divisor = (abs(area_list[divisor])/1000000)
    perimeter_km_divisor = (abs(perimeter_list[divisor])/1000)
    print(f"Area: {area_km_divisor}")
    print(f"Perimeter: {perimeter_km_divisor}")
    
    area_divid_div = area_km_dividend / area_km_divisor
    area_div_divid = area_km_divisor / area_km_dividend
    
    perimeter_divid_div = perimeter_km_dividend / perimeter_km_divisor
    perimeter_div_divid = perimeter_km_divisor / perimeter_km_dividend
    
    if area_divid_div > 1:
        print(f"\nArea of region #{dividend + 1} is", area_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        if perimeter_divid_div > 1:
            print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        else:
            print(f"Perimeter of region #{divisor + 1} es un", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
    else:
        print(f"\nArea of region #{divisor + 1} is", (area_div_divid*100) - 100, f"% larger than #{dividend + 1}")
        if perimeter_divid_div > 1:
            print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        else:
            print(f"Perimeter of region  #{divisor + 1} is", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
    
    if input('Do you want to evaluate more regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
        break

To know if the regions evaluated intercepts, and what percentage of the surface they do, I have added a zip that "joins" each latitude to its longitude value, and I have converted that zip in a list, so I can print it each time because it is in a loop (that calculates the number of areas the user wants):

from pyproj import Geod

geod = Geod(ellps='WGS84')

regions = 0
area_list = []
perimeter_list = []
pair_coordinates_list = []       #NEW
while True:
    regions += 1
    lats = []
    lons = []
    count = 0
    while True:
        count += 1
        lat = float(input(f"Enter latitude #{count}: "))
        lon = float(input(f"Enter longitude #{count}: "))
        lats.append(lat)
        lons.append(lon)
    
        if count < 3:
            print("Not enough information to calculate the area.")
            continue
    
        poly_area, poly_perimeter = geod.polygon_area_perimeter(lons, lats)

#NEW UNTIL...
        print(f"\nLatitude coordinates #{regions}: {lats}")
        print(f"Longitude coordinates #{regions}: {lons}")
              
        pair_coordinates = zip(lats,lons)
        type(pair_coordinates)
        pair_coordinates_list = list(pair_coordinates)
        print(f"Pair of coordinates #{regions}: ", pair_coordinates_list)
        
        for info in zip(lats,lons):
            print(info)
#....HERE
        print(f"Area #{regions}:", (abs(float(poly_area))) / (1000000), "km^2")
        print(f"Perimeter #{regions}:", (abs(float(poly_perimeter))) / (1000), "km")
        if input('Enter more coordinates? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
            area_list.append(poly_area)
            perimeter_list.append(poly_perimeter)
#            p1 = Polygon([lat lon])
            break 
    if input('More regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
        break 

while True:
    dividend = int(input(f"Write what region is the dividend:")) - 1
    area_km_dividend = (abs(area_list[dividend])/1000000)
    perimeter_km_dividend = (abs(perimeter_list[dividend])/1000)
    print(f"Area: {area_km_dividend}")
    print(f"Perimeter: {perimeter_km_dividend}")
    print(f"Pair of coordinates of region #{dividend + 1}: ", list(pair_coordinates_list))    #NEW
    
    divisor = int(input(f"Which region is the divisor?:")) - 1
    area_km_divisor = (abs(area_list[divisor])/1000000)
    perimeter_km_divisor = (abs(perimeter_list[divisor])/1000)
    print(f"Area: {area_km_divisor}")
    print(f"Perimeter: {perimeter_km_divisor}")
    print(f"Pair of coordinates of region #{divisor + 1}: ", list(pair_coordinates_list))        #NEW
    
    
    area_divid_div = area_km_dividend / area_km_divisor
    area_div_divid = area_km_divisor / area_km_dividend
    
    perimeter_divid_div = perimeter_km_dividend / perimeter_km_divisor
    perimeter_div_divid = perimeter_km_divisor / perimeter_km_dividend
    
    if area_divid_div > 1:
        print(f"\nArea of region #{dividend + 1} is", area_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        if perimeter_divid_div > 1:
            print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        else:
            print(f"Perimeter of region #{divisor + 1} es un", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
    else:
        print(f"\nArea of region #{divisor + 1} is", (area_div_divid*100) - 100, f"% larger than #{dividend + 1}")
        if perimeter_divid_div > 1:
            print(f"Perimeter of region #{dividend + 1} is", perimeter_divid_div*100 - 100, f"% larger than #{divisor + 1}")
        else:
            print(f"Perimeter of region  #{divisor + 1} is", perimeter_div_divid*100 - 100, f"% larger than #{dividend + 1}")
    
    if input('Do you want to evaluate more regions? [Y/N] ') in ['n', 'N', 'No', 'no', 'non']:
        break

For the first loop, it prints correctly each area, but when I call the pair_coordinates_list, it just prints the last list of a pair of coordinates calculated, for any number of regions evaluated.

It goes like this:

Enter latitude #1: 7

Enter longitude #1: 1
Not enough information to calculate the area.

Enter latitude #2: 8

Enter longitude #2: 4
Not enough information to calculate the area.

Enter latitude #3: 9

Enter longitude #3: 1

Latitude coordinates #1: [7.0, 8.0, 9.0]
Longitude coordinates #1: [1.0, 4.0, 1.0]
Pair of coordinates #1:  [(7.0, 1.0), (8.0, 4.0), (9.0, 1.0)]
(7.0, 1.0)
(8.0, 4.0)
(9.0, 1.0)
Area #1: 36586.21308722247 km^2
Perimeter #1: 918.6214017289213 km

Enter more coordinates? [Y/N] n

More regions? [Y/N] 3

Enter latitude #1: 4

Enter longitude #1: 8
Not enough information to calculate the area.

Enter latitude #2: 7

Enter longitude #2: 4
Not enough information to calculate the area.

Enter latitude #3: 6

Enter longitude #3: -8

Latitude coordinates #2: [4.0, 7.0, 6.0]
Longitude coordinates #2: [8.0, 4.0, -8.0]
Pair of coordinates #2:  [(4.0, 8.0), (7.0, 4.0), (6.0, -8.0)]
(4.0, 8.0)
(7.0, 4.0)
(6.0, -8.0)
Area #2: 242137.74313303368 km^2
Perimeter #2: 3673.4333319086 km

Enter more coordinates? [Y/N] y

Enter latitude #4: 1

Enter longitude #4: 0

Latitude coordinates #2: [4.0, 7.0, 6.0, 1.0]
Longitude coordinates #2: [8.0, 4.0, -8.0, 0.0]
Pair of coordinates #2:  [(4.0, 8.0), (7.0, 4.0), (6.0, -8.0), (1.0, 0.0)]
(4.0, 8.0)
(7.0, 4.0)
(6.0, -8.0)
(1.0, 0.0)
Area #2: 640301.386532112 km^2
Perimeter #2: 3881.491201974232 km

Enter more coordinates? [Y/N] n

More regions? [Y/N] n

Write what region is the dividend:2
Area: 640301.386532112
Perimeter: 3881.491201974232
Pair of coordinates of region #2:  [(4.0, 8.0), (7.0, 4.0), (6.0, -8.0), (1.0, 0.0)]

Which region is the divisor?:1
Area: 36586.21308722247
Perimeter: 918.6214017289213
Pair of coordinates of region #1:  [(4.0, 8.0), (7.0, 4.0), (6.0, -8.0), (1.0, 0.0)]

Area of region #2 is 1650.1165944822358 % larger than #1
Perimeter of region #2 is 322.5343753878306 % larger than #1

Do you want to evaluate more regions? [Y/N] n

"Pair of coordinates of region" line is the same for region #2 and #1, because it takes the last value from that list, which is the second area.

How can I solve it?

5

You assign zip(lats,lons) to pair_coordinates in every loop. Therefore, when you exit out of the loop, pair_coordinates and pair_coordinates_list always contain the coordinates of the last region you entered.

To solve this, define pair_coordinates_list as a dictionary.

pair_coordinates_list = {}

Then change all pair_coordinates_list as follows, respectively:

line 33    pair_coordinates_list[regions-1] = list(pair_coordinates)
line 34    print(f"Pair of coordinates #{regions}: ", pair_coordinates_list[regions-1])

           ...
line 55    print(f"Pair of coordinates of region #{dividend + 1}: ", list(pair_coordinates_list[dividend]))

           ...
line 62    print(f"Pair of coordinates of region #{divisor + 1}: ", list(pair_coordinates_list[divisor]))
1
  • Massive thanks to Kadir, I was stuck for a lot of hours. Won't forget the "dictionary" definition. Sep 3 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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