你们可以看到它有什么问题,因为我无法理解它吗?
def hanoi(disk, start='START', end='END', middle='MIDDLE'):
count = 0
busy = "true"
while busy == "true":
if disk > 0:
hanoi(disk - 1, start, middle, end)
print('Move disk' + str(disk) + ' from ' + start + ' to ' + end)
hanoi(disk - 1, middle, end, start)
count = count + 1
elif disk == 0:
busy = "false"
print (count)
if __name__ == '__main__':
hanoi(int(input('How many disks you wanna play? ')))
在询问了我的老师后,我发现了问题:正确的代码如下:
count = 0
def increment():
global count
count = count+1
def hanoi(disk, start='START', end='END', middle='MIDDLE'):
if disk > 0:
hanoi(disk - 1, start, middle, end)
print('Move disk' + str(disk) + ' from ' + start + ' to ' + end)
increment()
hanoi(disk - 1, middle, end, start)
if __name__ == '__main__':
hanoi(int(input('How many disks you wanna play? ')))
print (count)
感谢大家的帮助。
答案 0 :(得分:2)
让我们看看你写的功能。如果输入7,则hanoi(7)
是执行以下操作的函数:
count = 0
busy = True
count
count
(第一次到达此步骤时为1
)你看到了问题吗?