The problem is likely on line 13. Since sum1 is a global variable, it cannot be reset inside a function unless that function uses the keyword "global". In any case, global functions should not be used like that. Here's a rewrite of the code. The commented out section should work for you and lines 28 through 33 are a test for 6:
You may want to make factors into a set instead of list using my code. Otherwise, square numbers may not be correctly evaluated.
import math KB = 1024 MB = 1024 * 1024 def perfect_number(number, factors): sum_of_factors = sum(factors) - number if sum_of_factors == number: return f"The number {number} is a Perfect number!" else: return f"The number {number} is not a Perfect number!" def factor_number(n): root = int(round(math.sqrt(n), 0)) return [(x, n // x) for x in range(1, root + 1) if n % x == 0] if __name__ == '__main__': """with open('6.txt', buffering=1*MB) as bigggg: for num in bigggg: number = int(num) factors = [] for pair in factor_number(number): factors.extend(pair) print(perfect_number(number, factors))""" factors = [] for pair in factor_number(6): factors.extend(pair) print(perfect_number(6, factors))
You may want to make factors into a set instead of list using my code. Otherwise, square numbers may not be correctly evaluated.