Python complaints

skaller skaller at maxtal.com.au
Thu Nov 25 18:26:15 EST 1999


Brad Knotwell wrote:
> In general, the only thing I've found annoying about Python are the scoping
> rules:
> 
> >>> def fact(n):
> ..     def fact_iter(n,result):
> ..       if not n:  return result
> ..       else:      fact_iter(n-1,n*result)
> ..     return fact_iter(n,1)
> ..
> >>> fact(3)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 5, in fact
>   File "<stdin>", line 4, in fact_iter
> NameError: fact_iter

There's an error on your code (you have to 'return' in line 4).
After that correction, running on Viperi:

[root at ruby] ~/viper>./viperi
Viperi 2.0.1
Copyright Maxtal P/L, John Skaller, Australia, 1999

>>>def fact(n):
...   def fact_iter(n,result):
...     if not n:  return result
...     else:     return fact_iter(n-1,n*result)
...   return fact_iter(n,1)
...
>>>print fact(3)
...
6 
>>>print fact(7)
...
5040 

-- 
John Skaller, mailto:skaller at maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia
homepage: http://www.maxtal.com.au/~skaller
voice: 61-2-9660-0850




More information about the Python-list mailing list