[PLUG] Novice Python Error #6
spike spooner
spike at pcez.com
Wed Jul 13 01:22:56 UTC 2005
Rich Shepard wrote:
> I get the same error with this code whether I run it interactively
> or at
> the command line:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "combos.py", line 17
> num = factorial.factorial(m)
> ^
> SyntaxError: invalid syntax
>
> I don't know why the syntax is invalid.
>
> The files factorial.py and combos.py are attached. Factorial works from
> both the command line and interactively. The name of the variable is
> immaterial; 'top' or 'foo' produce the same error.
>
> Rich
>
>------------------------------------------------------------------------
>
>#!/usr/bin/python
>
>def factorial(n):
> if int(n == 0):
> return 1
> else:
> return n * factorial(n-1)
>
>if __name__ == '__main__':
> import sys
> n = int(sys.argv[1])
> print n * factorial(n-1)
>
>
>------------------------------------------------------------------------
>
>#!/usr/bin/python
>"""
> Calculates the number of combinations of 'm' things taken 'n' at a time.
>"""
>import factorial
>
>def combos(m, n):
> num = factorial.factorial(m)
> denom = factorial.factorial(n) * factorial.factorial(m-n)
> c = num / denom
> return c
>
>if '__name__' == '__main__':
> import sys, factorial
> m = int(sys.argv[1])
> n = int(sys.argv[2]
> num = factorial.factorial(m)
> denom = factorial.factorial(n) * factorial.factorial(m-n)
> c = num / denom
> print c
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>PLUG mailing list
>PLUG at lists.pdxlinux.org
>http://lists.pdxlinux.org/mailman/listinfo/plug
>
>
I took the single quotes of __name__ and added a right parenthesis
after argv[2] and your program worked.
--spike
More information about the PLUG
mailing list