【发布时间】:2018-12-26 21:03:08
【问题描述】:
我对 python 还很陌生,而且我已经对你们中的许多人可能会觉得小菜一碟提出了挑战。输出必须是:
The Monkey-child could not fall asleep, so the mother told him a story, he was once a Tiger-child
The Tiger-child could not fall asleep, so the mother told him a story, he was once a Human-child
The Human-child could not fall asleep, so the mother told him a story, he was once a Panther-child
The Panther-child could not fall asleep, so the mother told him a story, he was once a Snake-child
The Snake-child has tired and fell asleep
The Panther-child has tired and fell asleep
The Human-child has tired and fell asleep
The Tiger-child has tired and fell asleep
The Monkey-child has tired and fell asleep
修改代码如下(for 和while 不允许循环):
import sys
StorySequence = {
"Monkey": "Tiger",
"Tiger": "Human",
"Panther": "Snake",
"Snake": "",
"Human": "Panther"
}
def writePaddingForDepth(depth):
for i in range(0, depth):
sys.stdout.write(' ')
sys.stdout.flush()
def endStory(thread, depth):
writePaddingForDepth(depth)
print ("The " + thread + "-child has tired and fell asleep.")
return True
def startStory(thread, depth):
if (len(StorySequence[thread]) == 0):
return endStory(thread, depth)
writePaddingForDepth(depth)
print ("The " + thread + "-child could not fall asleep, "
"so the mother told him a story, he was once "
+ StorySequence[thread] + "-child")
## Code here
startStory("Monkey", 0)
如果它是 C 中的数组,我试图处理它,但显然不是,据我所知,它是 dict 类型,这对我来说是全新的东西。我想知道在这个例子中如何在没有for 或while 循环的情况下实现递归。
【问题讨论】: