关于作者

Discuss / Python / 添加了计算步数,但不显示字符串,哪里出问题了呢?

添加了计算步数,但不显示字符串,哪里出问题了呢?

Topic source

Wnleont

#1 Created at ...

def move(n,a,b,c): if n==1: print('move',a,'->',c) else: move(n-1,a,c,b) move(1,a,b,c) move(n-1,b,a,c) x=(2**n)-1 return x print('total steps:',x)

move(4,'a','b','c') move a -> b move a -> c move b -> c move a -> b move c -> a move c -> b move a -> b move a -> c move b -> c move b -> a move c -> a move b -> c move a -> b move a -> c move b -> c 15 为什么print结果只有x的值,而没有‘total steps:’字符串呢?

UMeowBing

#2 Created at ...

函数在return以后就结束了,只返回了x; 要执行打印语句: x = move(4,'a','b','c') print("叉叉叉:",x)


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.