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

去吧

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

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

目前,我收到 NameError: name 'recipient' is not defined。如果我注释掉 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()

这封信应该是这样的:

, 其他血清

.enif m'I uoy 时代 woH

! uy ssim I

, noitceffa dna evol ymlla htiW

oG-oG

! 埃拉克.SP

如果输入提示在 lkng_glss_lttr_bot 函数中,一切都会按我的预期工作。但我不能把头放在 main() 上。任何让我朝着正确方向前进的建议将不胜感激。谢谢!

员工

这里的工作代码

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章