Search code examples
pythonpython-3.xfunctionuser-inputmain-method

How to call my_function in main & prompt user for input in main method; my_function accepts user input & concatenates in reverse order (Python 3)


I need to create a function that does the following for a class assignment: - Write a Python function that will accept as input three string values from a user. The method will return to the user a concatenation of the string values in reverse order. The function is to be called from the main method. - In the main method, prompt the user for the three strings.

Originally, I had created a function (lkng_glss_lttr_bot) with a nested dictionary that would prompt the user for input (sections of a letter), store it in the dictionary, concatenate the entries and then return the reverse-ordered strings as a Looking Glass Letter of sorts. The function worked the way that I expected it to, and I was able to create a small program with loops so that the user could keep adding more entries. But once I added the main method, removed the input from lkng_glss_lttr_bot() and tried to put the input into main(), everything went haywire.

Currently, I'm getting NameError: name 'recipient' is not defined. If I comment out the get_recipient() function, then it goes on to the next nested function and gives me a NameError.

I'm missing something in my code that is allowing the variables to be named - but I'm a noob and I can't figure out where the issue lies.

I thought possibly the nested dictionary was the problem, so I simplified the function by taking out the dictionary, removing the loops, and terminating the program after one letter was composed. Again, the function worked while the input was in lkng_glss_lttr_bot(), but once I tried to put the input into main(), I couldn't get the program to execute.

The examples I've seen on SO suggest creating a separate function outside of main() to store info and have the user input in that separate function, but this won't work for the requirements of my assignment. Some people also suggested adding global variables. I tried that, and still received the same error.

I tried creating nested function for each variable, and returning the variable, but that also didn't work. I thought that nesting the functions within main would meet the requirements of having the user input in the main method, but I'm still getting a NameError.

I honestly don't understand main() at all, especially with user input because the examples I've seen don't have the input in main().

def main():
    lkng_glss_lttr_bot()


    def get_recipient():
        recipient = [input("\nWhat is the recipient’s name?\n")]
        return get_recipient

    def get_inquiry():
        print("\nPlease inquire about %s's well being." % recipient)
        inquiry = input("You can ask about the weather, or what %s has been doing to pass the time in the interim between your last contact:\n" % recipient)
        return inquiry

    def get_lttr_body():
        body = input("\nPlease write a few sentences to inform %s of the recent happenings in your life:\n" % recipient)
        return body

    def get_final_sentiment():
        final_sentiment = input("\nPlease close the letter by expressing the importance of your friendship and your desire to spend time with %s in the future:\n" % recipient )
        return final_sentiment

    def get_sender():
        sender = input("\nWhat is your name?\n")
        return sender

    def get_postscript():
        postscript = input("\nPlease write a short postscript to %s:\n" % recipient)
        return postscript  


# Function to create a personalized letter in reverse order 
def lkng_glss_lttr_bot():

    recipient = get_recipient()
    inquiry = get_inquiry()
    body = get_body()
    final_sentiment = get_final_sentiment()
    sender = get_sender()
    postscript = get_postscript()

    greeting = str("My Dearest")
    closing = str("With all my love and affection,")

    # Concatenate Greeting and Recipient
    Line_1 = greeting + " " + recipient + "," + "\n"

    # Concatenate Inquiry and Body
    Line_2 = inquiry + " " + body + "\n"

    # Concatenate Line_1, Line_2, Final_Sentiment, Closing, Sender, & Postscript to create a reverse ordered letter
    rvrs_lttr = "P.S. " + postscript + "\n" + sender + "\n" + closing + "\n" + final_sentiment + "\n" + Line_2 + Line_1

    # Using rvrs_lttr format, reverse the order of the entire letter using an extended slice to create a Looking Glass Letter
    lkng_glss_lttr = rvrs_lttr[::-1]

    # Notify sender that their letter contents have been added to the Bot 
    print("\nYour letter to %s has been composed by the Looking Glass Letter Bot:\n" % recipient + lkng_glss_lttr)

    if __name__ == '__main__': main()

The letter should look like this:

,aloL tseraeD yM

.enif m'I ?uoy era woH

!uoy ssim I

,noitceffa dna evol ym lla htiW

oG-oG

!em llaC .S.P

And everything works as I expect it to if the input prompts are in the lkng_glss_lttr_bot function. But I can't wrap my head around main(). Any suggestions to get me in the right direction would be appreciated. Thanks!


Solution

  • Here the working code:

    def main():
    
        recipient = ""
    
        def get_recipient():
            recipient = [input("What is the recipients name?")]
            return recipient
    
        def get_inquiry():
            print("\nPlease inquire about %s's well being." % recipient)
            inquiry = input("You can ask about the weather, or what %s has been doing to pass the time in the interim between your last contact:\n" % recipient)
            return inquiry
    
        def get_body():
            body = input("\nPlease write a few sentences to inform %s of the recent happenings in your life:\n" % recipient)
            return body
    
        def get_final_sentiment():
            final_sentiment = input("\nPlease close the letter by expressing the importance of your friendship and your desire to spend time with %s in the future:\n" % recipient )
            return final_sentiment
    
        def get_sender():
            sender = input("\nWhat is your name?\n")
            return sender
    
        def get_postscript():
            postscript = input("\nPlease write a short postscript to %s:\n" % recipient)
            return postscript  
    
        def lkng_glss_lttr_bot():  
            recipient = get_recipient()
            inquiry = get_inquiry()
            body = get_body()
            final_sentiment = get_final_sentiment()
            sender = get_sender()
            postscript = get_postscript()
    
            greeting = str("My Dearest")
            closing = str("With all my love and affection,")
    
            # Concatenate Greeting and Recipient
            Line_1 = greeting + " " + str(recipient) + "," + "\n"
    
            # Concatenate Inquiry and Body
            Line_2 = inquiry + " " + body + "\n"
    
            # Concatenate Line_1, Line_2, Final_Sentiment, Closing, Sender, & Postscript to create a reverse ordered letter
            rvrs_lttr = "P.S. " + postscript + "\n" + sender + "\n" + closing + "\n" + final_sentiment + "\n" + Line_2 + Line_1
    
            # Using rvrs_lttr format, reverse the order of the entire letter using an extended slice to create a Looking Glass Letter
            lkng_glss_lttr = rvrs_lttr[::-1]
    
            # Notify sender that their letter contents have been added to the Bot 
            print("\nYour letter to %s has been composed by the Looking Glass Letter Bot:\n" % recipient + lkng_glss_lttr)
    
        lkng_glss_lttr_bot()   
    
    
    if __name__ == '__main__': 
        main()