print文が何回呼ばれたかカウントできません。
# -*- coding: utf-8 -*- def hanoi(n, x, y, z): global count count = 0 if n == 1: count += 1 print("{}→{}".format(x, y)) else: hanoi(n-1, x, z, y) count += 1 print("{}→{}".format(x, y)) hanoi(n-1, z, y, x) print(hanoi(3, "A", "C", "B")) print(count)
とコードを書いて実行したら、1と出力されました。
print文は7回呼ばれているので、どうして
count += 1
ができないで1と出力されるのかがわかりません。
print文が何回呼ばれたかカウントするにはどう直せば良いのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/07/10 11:53
2017/07/10 11:55