1つの引数(数値)をとり、その数の階乗を返す関数を作成しようとしています。例えば、fのPythonのシーケンスの最初のn項の積
(5)* 5 * 4 * 3 1 * 2
私はこれまで
def product(n, term):
"""Return the product of the first n terms in a sequence.
term -- a function that takes one argument
"""
k, total = 1, 1
while k <= n:
k, total = k + 1, total * term(k, 1)
return total
def factorial(n):
"""Return n factorial by calling product.
>>> factorial(4)
24
"""
return product(n, mul)
でいることはしかし、それだけでその用語ので、それを行うことが可能であるが返されます。引数は1つかかりますか?
:その後、あなたの
factorial(n)
はアイデンティティが、他の言葉でdef identity(n): return n
ある
def factorial(n): return product(n, identity)
として定義されるだろうあなたの例では 'mul'とは何ですか? 'mul'が" multiply "を意味するなら、それはどのように一つの引数で動作するのですか? 'total * term(k、1)'ではなく 'term(total、k)'を意味するのではないですか?あなたは何をしようとしているのですか?これがどのように機能するのか、より詳細な説明を書くことができますか? –