math - This function should work to calculate factorials, but it doesn't. [python-3.x] -
(full disclosure, going through python tutorial @ codeacademy, , using web-based ide.)
def factorial(x): bang = 1 num in x: bang = bang * num return bang
in java, works generate factorial number smaller 2,147,483,647. think should work in python, doesn't. instead error:
"traceback (most recent call last): file "python", line 3, in factorial typeerror: 'int' object not iterable"
perhaps there's i'm not understanding here, or perhaps syntax wrong. tested further , created separate function called factorial iterates:
def factorial(x): if x > 2: return x else: return x(factorial(x-1))
this doesn't work, giving me error:
"traceback (most recent call last): file "python", line 11, in factorial typeerror: 'int' object not callable"
i python noob, seems both of these should work. please advise on best way learn python syntax...
you can't for num in x
if x
integer. integer isn't "iterable" error says. want this:
def factorial(x): bang = 1 num in xrange(1, x+1): bang = bang * num return bang
the xrange
(or range
) generate necessary range of numbers in
operate upon.
Comments
Post a Comment