What is the best practice for organizing a piece of reusable code? Python : I am building a text-based encryption and decryption game.  There are different levels, and each level uses a different cipher for encrypting a text.  I am trying to figure out the best practice for the series of questions and prompts (the narrative) I give the user to determine if he wants to practice, do the test, encrypt, or decrypt.  90% of the narrative is the same for each level, so I don't want to repeat myself with identical code.  What is the best way to do this?

My first thought was to define a function that contained the general script, and to call the specific functions as parameters.  (This is what I have attempted to do below).  But I seem to run into a scope problem.  When I call the caesar() function as one of the arguments in the script() function, I need to enter the text to be encryprted, but this text isn't provided by the user until the script() function has already started running.

Should I be using a Class to define the narrative portion of the program, and then inherit to more specific types?

Or should I just repeat the narrative code at the different levels? 

Here is the narrative script():


    def script(encrypt, decrypt):
        """Asks user if they want to practice (encode or decode) or take the 
        test, and calls the corresponding function."""
    encrypt = encrypt
    decrypt = decrypt

    while True:
        print('Type Q to quit.  Type M to return to the main menu.')
        prac_test = input('would you like to practice or take the test? P/T')
        if prac_test.lower() == 'p':
            choice = input('Would you like to encrypt or decrypt? E/D ')
            if choice.lower() == 'e':
                text = input('Enter the text you would like to encode: ')
                encrypt
            elif choice.lower() == 'd':
                text = input('Enter the text you would like to decode: ')
                key = int(input('Enter the key: '))
                decrypt
            else:
                print('You must enter either "E" or "D" to encode or decode a text. ')
        elif prac_test.lower() == 't':
            text = random.choice(text_list)
            encrypted_text = encrypt
            print(encrypted_text[0])
            answer = input('s/nCan you decode this string? ')
            if answer.lower() == ran_str.lower():
                print('Congrats! You solved level 1!\n')
                pass
            elif answer != ran_str:
                print("Sorry, that's not correct.  Why don't you practice some more?\n")
                script(encrypt, decrypt)
        elif prac_test.lower() == 'q':
            exit()
        elif prac_test.lower() == 'm':
            break
        else:
            print('Please enter a valid choice.')

Here is one of the levels using a caesar cipher:

    def caesar(mode, text, key=None):
        """
     ...
    The dictionaries that convert between letters and numbers are stored in the .helper file, imported above.
        """
        mode = mode
        if mode == 'encrypt':
            key = random.randint(1, 25)
        elif mode == 'decrypt':
            key = key
        str_key = str(key)
        text = text.lower()
        # converts each letter of the text to a number
        num_list = [alph_to_num[s] if s in alph else s for s in text]
        if mode == 'encrypt':
            # adds key-value to each number
            new_list = [num_to_alph[(n + key) % 26] if n in num else n for n in 
    num_list]
        elif mode == 'decrypt':
            # subtracts key-value from each number
            new_list = [num_to_alph[(n - key) % 26] if n in num else n for n in 
    num_list]
        new_str = ''
        for i in new_list:
            new_str += i
        return new_str, str_key

And here is who I would try to use them together:

    script(caesar('encrypt' text), caesar('decrypt', text, key))

Please instruct me on the best way to organize this reusable narrative code.  Thank you.

