Python simple Code

Denis McMahon denismfmcmahon at gmail.com
Sat Jan 24 19:38:07 EST 2015


On Sat, 24 Jan 2015 16:16:16 -0800, Salem Alqahtani wrote:

> import sys
> import array

> a=['salem','Ali','sultan']
> m = len(a)

> def Factorials(m):
>     if m == 0:
>         return 1
>     else:
>         print m
>         return m * Factorials(m-1)

> def output():
>     print a

> def main():
>     print Factorials(m)
>     output()

> main()

When I run the program, the output is:

3
2
1
6
['salem', 'Ali', 'sultan']

I'm not sure which output you think is wrong, or is not doing what you 
expect.

Observations:

a) There is no need to import array, you are only using a list.
b) If you try and index into a list using a number greater than the list 
length, you will receive an error "IndexError: list index out of range", 
hence if you have a list where len(list) is x items you will never be 
able to use x! as an index to the list.
c) To refer to a single member of a list, use list[n] where n is the zero 
indexed element you wish to refer to.
d) Note point b. l[len(l)!] (or l[Factorials(len(l))]) will always fail.

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list