# 递归函数

```while True:
story = "
从前有个山，山里有座庙，庙里老和尚讲故事，
讲的什么呢？
"
print(story)```

```def story():
s = """
从前有个山，山里有座庙，庙里老和尚讲故事，
讲的什么呢？
"""
print(s)

while True:
story()```

```def story():
s = """
从前有个山，山里有座庙，庙里老和尚讲故事，
讲的什么呢？
"""
print(s)
story()

story()```

```def foo(n):
print(n)
n += 1
foo(n)
foo(1)```

```import sys
print(sys.setrecursionlimit(100000))```

 1 金鑫 40 2 武sir 42 3 egon 44 4 alex 46

```age(4) = age(3) + 2
age(3) = age(2) + 2
age(2) = age(1) + 2
age(1) = 40```

```def age(n):
if n == 1:
return 40
else:
return age(n-1)+2

print(age(4))```

```menu = {
‘北京‘: {
‘海淀‘: {
‘五道口‘: {
‘soho‘: {},
‘网易‘: {},
},
‘中关村‘: {
‘爱奇艺‘: {},
‘汽车之家‘: {},
‘youku‘: {},
},
‘上地‘: {
‘百度‘: {},
},
},
‘昌平‘: {
‘沙河‘: {
‘老男孩‘: {},
‘北航‘: {},
},
‘天通苑‘: {},
‘回龙观‘: {},
},
‘朝阳‘: {},
‘东城‘: {},
},
‘上海‘: {
‘闵行‘: {
"人民广场": {
‘炸鸡店‘: {}
}
},
‘闸北‘: {
‘火车战‘: {
‘携程‘: {}
}
},
‘浦东‘: {},
},
‘山东‘: {},
}```
``` 1 def threeLM(dic):
2     while True:
3         for k in dic:print(k)
4         key = input(‘input>>‘).strip()
5         if key == ‘b‘ or key == ‘q‘:return key
6         elif key in dic.keys() and dic[key]:
7             ret = threeLM(dic[key])
8             if ret == ‘q‘: return ‘q‘

9

```l = [menu]
while l:
for key in l[-1]:print(key)
k = input(‘input>>‘).strip()   # 北京
if k in l[-1].keys() and l[-1][k]:l.append(l[-1][k])
elif k == ‘b‘:l.pop()
elif k == ‘q‘:break```

http://www.cnblogs.com/Eva-J/articles/7197403.html

(0)
(0)

© 2014 mamicode.com 版权所有 京ICP备13008772号-2