求教求教!
前辈们,请教下,为什么if后有print和return,这个我能理解,但是下边三个move后不需要再加print和return!?上边的print是if后代码块的内容,按照理解,如果n不等于1,就不执行if后的代码块,计算机是如何理解按照if后的print执行打印和返回的!?
前辈们,请教下,为什么if后有print和return,这个我能理解,但是下边三个move后不需要再加print和return!?上边的print是if后代码块的内容,按照理解,如果n不等于1,就不执行if后的代码块,计算机是如何理解按照if后的print执行打印和返回的!?
2019-01-04
def move(n, a, b, c): print n,a,b,c if n ==1: print a, '-->', c return move(n-1, a, c, b) print 'one',n-1,a,c,b move(1,a,b,c) print 'two',1,a,b,c move(n-1, b, a, c) print 'three',n-1,b,a,c move(4, 'A', 'B', 'C') 运行结果: 4 A B C 3 A C B 2 A B C 1 A C B A --> B one 1 A C B 1 A B C A --> C two 1 A B C 1 B A C B --> C three 1 B A C one 2 A B C 1 A C B A --> B two 1 A C B 2 C A B 1 C B A C --> A one 1 C B A 1 C A B C --> B two 1 C A B 1 A C B A --> B three 1 A C B three 2 C A B one 3 A C B 1 A B C A --> C two 1 A B C 3 B A C 2 B C A 1 B A C B --> C one 1 B A C 1 B C A B --> A two 1 B C A 1 C B A C --> A three 1 C B A one 2 B C A 1 B A C B --> C two 1 B A C 2 A B C 1 A C B A --> B one 1 A C B 1 A B C A --> C two 1 A B C 1 B A C B --> C three 1 B A C three 2 A B C three 3 B A C
举报