0

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!

Go-Go
  • 17
  • 5
  • Please see my comment about the input and concatenation. The input is not in main, and the concatenation is not in my function. Give me a second to mess around with this - I have a feeling that using global and then having recipient = '' outside of the functions might be the step that I was missing. – Go-Go Jan 19 '19 at 20:20
  • I appreciate all your help, but this still isn't quite what I need. I have to have my own function (lkng_glss_lttr_bot) as well. lkng_glss_lttr_bot has the formulas for concatenating the strings. Main has the input for the variable in the formula, and lkng_glss_lttr_bot must be called from main. You solution removed lkng_glss_lttr_bot and main doesn't call on lkng_glss_lttr_bot. The exact directions for the assignment are in the first paragraph of my question. Thank you for your help with this! – Go-Go Jan 19 '19 at 20:31
  • I just now posted the overall final correct answer! Did you read it? Tried it? Does it now REALLY works as expected? – Employee Jan 19 '19 at 20:37
  • If you see now is how you want it: lkng_glss_lttr_bot() call is in the main function :-) – Employee Jan 19 '19 at 20:41
  • Could you please take your time to tell me if the final answer did the job correctly? If so, mark it as accepted please – Employee Jan 19 '19 at 20:42
  • Yes! You did it! Thank you so much. There is only one issue, but it's outside of the scope of my original question, but I'm hoping you'll know why it's happening. With the input, I used %s and % recipient to fill in the recipient name in the input questions. Now that the program works according to the specifications, the recipient name is no longer being printed, except at the end where it notifies the user the the letter has been composed. The name in the print prompt and in the letter gets printed, but when it's used for the other functions it doesn't show up. Any idea why? Again, THANKS! – Go-Go Jan 19 '19 at 20:53

1 Answers1

1

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()
Employee
  • 3,109
  • 5
  • 31
  • 50
  • I tried using this code, but now I'm getting a Name Error for raw_input: NameError: name 'raw_input' is not defined. Are you using Python 2? My question was for Python 3, so I'm assuming I can change raw_input to input and this will work? – Go-Go Jan 19 '19 at 20:10
  • I'm editing the answer with the working code for Python3 where raw_input() is simply called input() – Employee Jan 19 '19 at 20:13
  • Also, you moved the functions out of main and added the concatenation to main - but my assignment specifies that the input must be in main, and that the concatenation must be done in my function. This is where I'm having trouble. I can make it work if the input is not in main, but when it's in main, it doesn't work for me. – Go-Go Jan 19 '19 at 20:16
  • Ok, so I am giving you now the code that works the way your teacher wants and I am attaching the explanation – Employee Jan 19 '19 at 20:19