Latest web development tutorials

Python includes exercises 28

Python 100 Li Python 100 Li

Title: Five people sitting together, the fifth person to ask how old? He said that over the first four individuals 2 years older. Q. The first four individuals age, he said, is larger than the first three individuals 2 years of age. Asked a third person, said the NPC than the second two years. Q. The first two individuals, say two years older than the first man. Finally, ask the first person, he said, he is 10 years old. Will the fifth person how much?

Program analysis: recursive method, recursive and recursive pushed back into two stages. To know the fifth person age, the need to know the age of the fourth person, and so on, to push the first person (10 years old), Zaiwang Hui push.

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def age(n):
    if n == 1: c = 10
    else: c = age(n - 1) + 2
    return c
print age(5)

The above example output is:

18

Python 100 Li Python 100 Li