Python Forum
Help with try and open 6.txt file and print as perfect or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with try and open 6.txt file and print as perfect or not
#1
This is the output I get in quotes with the below python code, however 6 is in the 6.txt file. How can I determine by print output if 6 is a perfect number or not? I know it is a perfect number!

"The number is not a Perfect number!
None"



Please Help I'm on to something big with easily determining Mersenne Prime Numbers. Once I have the below code fixed I can open large perfect numbers and then with my other py file I can differentiate between what numbers are perfect and not. The other code I will post here too after this is resolved and I will explain how its done. My other code is super fast at finding large perfect numbers!




KB = 1024
MB = 1024 * 1024
 
def perfect_number(file):
    
    for s in file:
        n = int(s)
        
sum1 = 0
def n():
 for i in range(1, n):
    if(n % i == 0):
        sum1 = sum1 + i
if (sum1 == n):
    print ("The number is a Perfect number!" )
else:
    print("The number is not a Perfect number!")
 
if __name__ == '__main__':
    with open('6.txt', buffering=1*MB) as bigggg:
        dr = file_perfect_number(bigggg)
    print(dr)
Reply
#2
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:

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.
Reply
#3
I'm getting this error. I still need help thanks.

Traceback (most recent call last):
File "C:\Python27\perfect numbers cheker.py", line 19, in <module>
with open('c:\python27\6.txt', buffering=1*MB) as bigggg:
IOError: [Errno 22] invalid mode ('r') or filename: 'c:\\python27\x06.txt'
Reply
#4
That's curious. I'm not sure why it inserted "x0" into your file name. Backslashes are an escape character in strings so that would be the likely culprit. Try forward slashes instead:

with open('c:/python27/6.txt', buffering=1*MB) as bigggg:
or double backslashes:

with open('c:\\python27\\6.txt', buffering=1*MB) as bigggg:
Reply
#5
Hi thank you for the help but now the output is>

None
None
Reply
#6
Can you post a few lines from the file? It may be an encoding issue as well.
Reply
#7
I had to define f
def f(): thats all and here is the code.

import math
 
KB = 1024
MB = 1024 * 1024
 
def perfect_number(number, factors):
    sum_of_factors = sum(factors) - number
    if sum_of_factors == number:
        def f():
         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))
Reply
#8
Ah, you must not be using the latest version. F-string were added recently. Instead of defining f(), use these lines.

def perfect_number(number, factors):
    sum_of_factors = sum(factors) - number
    if sum_of_factors == number:
        return "The number {} is a Perfect number!".format(number)
    else:
        return "The number {} is not a Perfect number!".format(number)
Reply
#9
Thank you that was great it works on smaller perfect numbers, however on large ones it returns this error. What is f string for? Does it allow for larger perfect numbers?

2**89-1*(2**88)

This is the perfect number it returned an error on.

191561942608236107294793378084303638130997321548169216



Traceback (most recent call last):
File "C:\Python27\perfect numbers cheker.py", line 23, in <module>
for pair in factor_number(number):
File "C:\Python27\perfect numbers cheker.py", line 15, in factor_number
return [(x, n // x) for x in range(1, root + 1) if n % x == 0]
OverflowError: range() result has too many items
Reply
#10
Since you're using an archaic version of python, there's things you need to keep an eye on and change when you see them in code. For example, whenever someone says range(), you should use xrange() instead. Since that version of range lazily generates a sequence, it'll still work with massive ranges.

If you have a choice, you should move to a version of python that isn't about to leave it's bugfix maintenance cycle: https://pythonclock.org/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 333 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,133 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 378 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  How can i combine these two functions so i only open the file once? cubangt 4 867 Aug-14-2023, 05:04 PM
Last Post: snippsat
  Start print a text after open an async task via button Nietzsche 0 709 May-15-2023, 06:52 AM
Last Post: Nietzsche
  I cannot able open a file in python ? ted 5 3,364 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,391 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  Saving the print result in a text file Calli 8 1,800 Sep-25-2022, 06:38 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,411 May-06-2022, 01:44 PM
Last Post: Larz60+
  failing to print not matched lines from second file tester_V 14 6,093 Apr-05-2022, 11:56 AM
Last Post: codinglearner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020