The Marine Biologist’s Python Challenge

For this challenge we will write a program that would be useful for a team of marine biologists working on a conservation project along the coast. Their aim is to estimate the population of sea turtles living in a particular bay. Since it’s impossible to count every single turtle in the water, the biologists use a clever technique known as the capture-recapture method.

Your challenge is to create a Python program that helps our team of marine biologists estimate the total sea turtles population based on data collected during two observation trips.

What Is the Capture-Recapture Method?

The capture-recapture method is commonly used by wildlife researchers to estimate animal populations without counting every individual.

Here’s how it works:

  1. First Capture: A group of animals is captured, marked (e.g. tagged), and released.
  2. Second Capture: Later, another group is captured, and researchers count how many are already marked.
  3. Using the overlap between the two groups (the number of animals marked within the second capture), we can estimate the total population with the following formula:

So let’s use the following example to better understand how this formula works:
During their first trip, the marine biologists catch and tag 80 sea turtles before releasing them.
On a second trip a few weeks later, they catch 110 sea turtles, 25 of which are already tagged.

Using this information, we can apply the capture-recapture formula to estimate the total population of sea-turtles living in the bay:

Python Challenge

Your task is to write a Python program that:

    Asks the user to input:

      The number of turtles tagged in the first capture.
      The number of turtles caught in the second capture.
      The number of recaptured tagged turtles in the second sample.

    Applies the capture-recapture formula.
    Outputs the estimated total population of sea turtles.

Python Code

Ready to dive in? Start coding and help the marine biologists make a difference!

Testing

Once your code is done, complete the following tests to check that your code is working as it should:

Test # Input Values Expected Output Actual Output
#1 First Capture: 80
Second Capture: 110
Recaptures: 25
Estimated Population Size: 352
#2 First Capture: 90
Second Capture: 70
Recaptures: 12
Estimated Population Size: 525
#3 First Capture: 70
Second Capture: 50
Recaptures: 0
We cannot estimate the population size with this data (0 recaptures)

Why It Matters

The capture-recapture approach is widely used by marine biologists, ecologists, and conservationists to study animal populations in a humane and efficient way. By writing a Python program for this method, you’re not only learning about real-world data science, but also supporting work that protects endangered species!

Extra Challenge

The team of marine biologists would like to improve the accuracy of their sea turtles population size estimate. To do so, they have decided to conduct this capture/recapture experiment six times, once every week over a period of 6 weeks. They have recorded their data in a file called Sea-Turtles.csv as follows:

first capture,second capture,number of retakes
80,110,25
76,70,16
90,70,19
98,64,18
82,72,18
85,78,20

We have saved this csv file as a separate tab in the Python IDE below.

The following Python code can be used to read our CSV file line by lineand extract the data from each line:

file = open("Sea-Turtles.csv","r")

for line in file:
   data = line.split(",")
   firstCapture = int(data[0])  
   secondCapture = int(data[1])
   retakes = int(data[2])
   print("First Capture: " + str(firstCapture) + " - Second Capture: " + str(secondCapture) + " - Retakes: " + str(Retakes))
  
file.close()

Your task is to tweak the following code to calculate the estimated population size for each experiment and work out the average population size over these 6 experiments.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!