1. Effect picture:

2. Key codes

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# define the function app
def app(a, b) :
    # Count the chickens
    x = (4 * a - b) / 2
    # count the number of heads > 0, and the total number of feet is not less than 2 times the number of heads (all chickens), not more than 4 times the number of heads (all rabbits).
    if a > 0 and 4 * a >= b and 2*a <=b:
        # Count rabbits
        y = a - x
        If either of the calculated chickens or rabbits is less than zero, an error is reported
        if x < 0 or y < 0:
            print("No solution for {} animal {} legs".format(a, b))
        # When neither chicken nor rabbit is less than zero, print the result
        else:
            print("Chickens have {}, rabbits have {}".format(int(x), int(y)))
    Stickline (a > 0 and 4 *a >= b and 2*a <=b)
    else:
        print("No solution for {} animal {} legs".format(a, b))

# prompt for the number of headers and feet at runtime
a = input("Please enter the total number of chickens and rabbits \n")
b = input("Please enter the total number of feet of chicken and rabbit \n")

Set the number of chickens and rabbits to an integer
a = int(a)
b = int(b)

# run function
app(a, b)
Copy the code

3. Preparation: Install the software first.

1) Install Python on a Mac

If you’re using a Mac and you’re running OS X>=10.9, the built-in Python version is 2.7. To install the latest Python 3.8, there are two methods:

Method 1: Download the Python 3.8 installation program from the Python official website, double-click it, and install it.

Method 2: If Homebrew is installed, run brew install python3 to install it.

2) Install Python on Linux

If you are using Linux, I can assume that you have Linux system administration experience and should have no problem installing Python 3 yourself. Otherwise, switch back to Windows.

For those of you who are currently using Windows and have no plans to switch to a Mac any time soon, read on.

3) Install Python on Windows

First, download the Python 3.8 64-bit or 32-bit installer from Python’s official website, depending on your Version of Windows (64-bit or 32-bit). Then, run the downloaded exe installation package:

In particular, check Add Python 3.8 to PATH and click Install Now to complete the installation.

Then test whether Python is installed successfully

After the installation is successful, open a command prompt window. Take Window as an example, press Win (usually on the left side of Alt), enter CMD, and enter


Enter Python and if 3.9.x is displayed, the installation is successful

Add Python 3.8 to PATH was omitted during the installation

┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ Command Prompt - - │ x ├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │ Microsoft Windows [Version 10.0.0] │ │ (c) 2015 Microsoft Corporation. All rights reserved. │ │C:\> python │'python' is not recognized as an internal or external co│ │ mmand, Operable program or batch file. │ │ │ │ C: \ > _ │ │ │ │ │ │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

Python text editor installed

Download the address and install to open: code.visualstudio.com/

4. Code writing

1. Create a Python file

Change the text ending in.py with the name python.py, as shown in the figure. Drag the python.py file onto the newly installed software or right-click to open it

2. Input the code part of the chicken and rabbit cage:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# define the function app
def app(a, b) :
    # Count the chickens
    x = (4 * a - b) / 2
    # count the number of heads > 0, and the total number of feet is not less than 2 times the number of heads (all chickens), not more than 4 times the number of heads (all rabbits).
    if a > 0 and 4 * a >= b and 2*a <=b:
        # Count rabbits
        y = a - x
        If either number of chickens or rabbits is less than zero, an error is reported
        if x < 0 or y < 0:
            print("No solution for {} animal {} legs".format(a, b))
        # When the number of rabbits is not less than zero, print the result
        else:
            print("Chickens have {}, rabbits have {}".format(int(x), int(y)))
    Stickline (a > 0 and 4 *a >= b and 2*a <=b)
    else:
        print("No solution for {} animal {} legs".format(a, b))

# prompt for the number of headers and feet at runtime
a = input("Please enter the total number of chickens and rabbits \n")
b = input("Please enter the total number of feet of chicken and rabbit \n")

Set the number of chickens and rabbits to an integer
a = int(a)
b = int(b)

# execute
app(a, b)
Copy the code

Analyze the code implementation steps:

A is the number of heads, b is the number of feet, x is the number of chickens, and y is the number of rabbits

  • Define the function app(a,b), use def statement, two parameters a and b are the number of heads and feet respectively

  • Calculate the number of chickens: assuming all rabbits, that is 4a feet, now B feet, the number of chickens x = (4 * a – b) / 2

  • A > 0 and 4 *a >= b and 2*a <=b;

    Make sure the number of chickens is greater than 0, the number of feet is greater than 2 times (all chickens) and less than 4 times (all rabbits).

  • A > 0 and 4 *a >= b and 2*a <=b

  • Format (int(x), int(y)) print(” {}, {} “. Format (int(x), int(y)))

    • Here we use the print() function to print out python results

    • Format (int(x), int(y)) is used to accept two arguments

    • The int() function takes an integer

3. Tap Terminal to open a new terminal

My python.py code location is D:\software, as shown here. Type CD D:\software at the terminal

4. After entering the file path, run the Python file and enterpython python.py

cdPy Please enter the total number of chickens and rabbits 30 Please enter the total number of feet of chickens and rabbits 100 There are 10 chickens and 20 rabbitsCopy the code

First Python chicken and rabbit cage implementation!