[Tutor] Bothersome NoneType Error for a List object

Osemeka Osuagwu abasiemeka at gmail.com
Wed Jul 4 18:01:34 CEST 2012


Hi,
I am trying to find the smallest positive number that is divisible by all
of the numbers from 1 to 20 without a remainder. I wrote the following code
(implementation of a solution method found in
http://en.wikipedia.org/wiki/Least_common_multiple#A_method_using_a_table )
but kept getting an error message (also posted) when I ran it. I can't
figure out the problem. I would appreciate help with this. Also, any hint
on how to make the code more elegant is welcome.

Regards,
Abasiemeka

*CODE*
def checkdiv(x, testlist):      #returns True if x can divide ANY member of
testlist,returns False otherwise
    for k in testlist:
        if k%x == 0:
            return True
    return False

def lcm(numlist):               #continuously divides numlist by each in a
list of prime numbers till cannot
    primeslist = [2, 3, 5, 7, 11]
    templist = []

    for prime in primeslist:
        if checkdiv(prime, numlist) == True:
            templist = templist.append(prime)
            for i in range(0,len(numlist)):
                if numlist[i]%prime == 0:
                    numlist[i] = numlist[i]/prime
        else:
            pass

    lcm = reduce(lambda x, y: x*y, templist)     #my first lambda
expression! (multiply all members of templist
    return lcm

print lcm([i for i in range(1, 21)])

*ERROR OUTPUT*
Traceback (most recent call last):
  File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE
University\Python\Python Code\MyCode\Project Euler code\Project Euler
answer 5.py", line 31, in <module>
    print lcm(first20)
  File "C:\Windows.old\Users\Abasiemeka\Abasiemeka\GOOGLE
University\Python\Python Code\MyCode\Project Euler code\Project Euler
answer 5.py", line 20, in lcm
    templist = templist.append(prime)
AttributeError: 'NoneType' object has no attribute 'append'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120704/1cac9413/attachment.html>


More information about the Tutor mailing list