如何在主方法中调用my_function并提示用户在主方法中进行输入; my_function接受用户输入并以相反的顺序连接(Python 3)

时间:2019-01-19 19:50:01

标签: python python-3.x function user-input main-method

我需要创建一个对类分配执行以下操作的函数: -编写一个Python函数,该函数将接受用户的三个字符串值作为输入。该方法将以相反的顺序将字符串值的串联返回给用户。该函数将从main方法中调用。 -在main方法中,提示用户输入三个字符串。

最初,我用嵌套词典创建了一个函数(lkng_glss_lttr_bot),该词典将提示用户输入(字母的各个部分),将其存储在词典中,连接条目,然后将反向顺序的字符串作为窥镜的信。该函数按我期望的方式工作,并且我能够创建一个带有循环的小程序,以便用户可以继续添加更多条目。但是一旦添加了main方法,从lkng_glss_lttr_bot()中删除了输入,并试图将输入放入main()中,一切就变得一团糟。

当前,我收到NameError:未定义名称“收件人”。如果我注释掉get_recipient()函数,那么它将继续进行下一个嵌套函数,并给我一个NameError。

我在代码中缺少允许对变量进行命名的内容-但是我是菜鸟,我不知道问题出在哪里。

我认为嵌套字典可能是问题所在,因此我简化了功能,取出字典,删除循环,并在组成一个字母后终止程序。同样,该函数在输入位于lkng_glss_lttr_bot()中时起作用,但是一旦尝试将输入放入main()中,就无法使程序执行。

我在SO上看到的示例建议在main()之外创建一个单独的函数来存储信息,并在该单独的函数中提供用户输入,但这不适用于我的分配要求。有人还建议添加全局变量。我尝试过,但仍然收到相同的错误。

我尝试为每个变量创建嵌套函数,然后返回该变量,但这也不起作用。我以为将函数嵌套在main中可以满足在main方法中让用户输入的要求,但是我仍然遇到NameError。

老实说,我根本不理解main(),尤其是在用户输入时,因为我看到的示例在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()

这封信应该看起来像这样:

,alal tseraeD yM

.enif我是世界上的那个时代

!uo ssim I

,诺伊奇夫娜脱氧核糖核酸酶

oG-oG

!em llaC .S.P

如果输入提示位于lkng_glss_lttr_bot函数中,那么一切都会按我的预期进行。但是我不能把头缠在main()上。任何建议,以正确的方向,我将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

这是工作代码

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()