2

Preface: I realize this code is finding Fibonacci numbers, not prime numbers.

Question: For some reason, the savework() function is not working. When I run a python interactive session and repeatedly run get_next_prime(), it produces the next number in the sequence as intended within that session. However, when I close the session and open a new one, it should remember the prior entries (because of pickle.dump etc.) but doesn't. It should be calling the pickled byte files each run. Why isn't it?

I'm a beginner: Please be gentle. Thank you!

Code:

# Write a function which, for each run, returns the smallest positive prime number larger than the one returned previously. 


""" Find and maintain a cumulative list of Prime numbers """ 


import pickle 

import atexit 


try: 
with open("dict_of_primes","rb") as file1, \
    open("last_prime_index_found","rb") as file2: 
        dict_of_primes = pickle.load(file1) 
        last_prime_index_found = pickle.load(file2) 

# Start fresh if either file could not be found 
except FileNotFoundError: 
    dict_of_primes = {} 
    last_prime_index_found = -1 

def get_next_prime(): 
    """ Get the next prime number """ 
    global last_prime_index_found 
    n = last_prime_index_found + 1 

    if n in [0,1]: 
        dict_of_primes[n] = n 

    else:
        dict_of_primes[n] = \
            dict_of_primes[n-1] +\
            dict_of_primes[n-2] 

    last_prime_index_found = n 
    return dict_of_primes[n] 

def save_work(): 
    """ Saves data for next session """ 
    with open("dict_of_primes","wb") as file1, \
        open("last_prime_index_found","wb") as file2:
            pickle.dump(dict_of_primes,file1) 
            pickle.dump(last_prime_index_found,file2)

# This will insure that save_work() is called 
# when you end your session     

atexit.register(save_work)

Python Interactive Sessions:

`C:\Users\Fiona Murphey\Desktop>python`

`Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32`

`Type "help", "copyright", "credits" or "license" for more information.`

`>>> from lastprime import *`

`>>> get_next_prime()`

`0`

`>>> get_next_prime()`

`1`

`>>> get_next_prime()`

`1`

`>>> get_next_prime()`

`2`

`>>> get_next_prime()`

`3`

`>>> get_next_prime()`

`5`

`>>> save_work()`

`>>> exit()`

In terminal:-

C:\Users\Fiona Murphey\Desktop>python
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32

    Type "help", "copyright", "credits" or "license" for more information.
>>> from lastprime import *

>>> get_next_prime()

0

>>>
  • You're dumping both of your objects to `file2`, nothing to `file1`. I would expect this to produce an EOFError when trying to load from the empty file1 on the next run, so I guess something else is going wrong too. To debug, *look at the files* - did they actually get created? Do they have a non-zero length? – jasonharper Nov 21 '17 at 13:15
  • I changed it to dump what I meant to dump into `file 1` into `file 1` and IT WORKED! THANK YOU!! @jasonharper – karathrace99 Nov 21 '17 at 13:20

0 Answers0