Help with try and open 6.txt file and print as perfect or not - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with try and open 6.txt file and print as perfect or not (/thread-15045.html) Pages:
1
2
|
Help with try and open 6.txt file and print as perfect or not - Pleiades - Dec-31-2018 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) RE: Help with try and open 6.txt file and print as perfect or not - stullis - Jan-01-2019 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. RE: Help with try and open 6.txt file and print as perfect or not - Pleiades - Jan-02-2019 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' RE: Help with try and open 6.txt file and print as perfect or not - stullis - Jan-02-2019 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: RE: Help with try and open 6.txt file and print as perfect or not - Pleiades - Jan-03-2019 Hi thank you for the help but now the output is> None None RE: Help with try and open 6.txt file and print as perfect or not - stullis - Jan-03-2019 Can you post a few lines from the file? It may be an encoding issue as well. RE: Help with try and open 6.txt file and print as perfect or not - Pleiades - Jan-03-2019 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)) RE: Help with try and open 6.txt file and print as perfect or not - stullis - Jan-03-2019 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) RE: Help with try and open 6.txt file and print as perfect or not - Pleiades - Jan-03-2019 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 RE: Help with try and open 6.txt file and print as perfect or not - nilamo - Jan-03-2019 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/ |