Finding a set of residual modules

Question:

I try to make a program that find the residual clases module 1000 of a number multiplied by itself.

The code is this:

def calculeClass(num, mod):
    res = num
    resClass = []
    while (res not in resClass):
        resClass = resClass.append(res)
        res = (res*num)% mod
    print(resClass)

calculeClass(7,100)

This should give me: [7,49,43,1]

However this code give me:
Traceback (most recent call last):
File “num.py”, line 9, in
calculeClass(7,100)
File “num.py”, line 4, in calculeClass
while (res not in resClass):
TypeError: argument of type ‘NoneType’ is not iterable

But I’m using this https://thispointer.com/python-how-to-check-if-an-item-exists-in-list-search-by-value-or-condition/.at

What is happening?

Answers:

append does not return a list; the list you call it on is mutated in place.

while res not in resClass:
    resClass.append(res)
    res = (res * num) % mod
Answered By: chepner
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.