如何在 main 中调用 my_function 并提示用户在 main 方法中输入; my_function 接受用户输入并以相反的顺序连接 (Python 3)
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)
我需要创建一个函数来为 class 赋值执行以下操作:
- 编写一个 Python 函数,它将接受来自用户的三个字符串值作为输入。该方法将 return 以相反顺序连接到用户的字符串值。该函数将从 main 方法中调用。
- 在 main 方法中,提示用户输入三个字符串。
最初,我创建了一个带有嵌套字典的函数 (lkng_glss_lttr_bot),它会提示用户输入(字母的各个部分),将其存储在字典中,连接条目,然后 return 反向排序的字符串作为各种 Looking Glass Letter。该函数按我预期的方式工作,我能够创建一个带有循环的小程序,以便用户可以继续添加更多条目。但是一旦我添加了 main 方法,从 lkng_glss_lttr_bot() 中删除了输入并尝试将输入放入 main() 中,一切都变得一团糟。
目前,我收到 NameError:名称 'recipient' 未定义。如果我注释掉 get_recipient() 函数,那么它将继续到下一个嵌套函数并给我一个 NameError。
我的代码中遗漏了允许命名变量的内容 - 但我是菜鸟,我不知道问题出在哪里。
我认为可能是嵌套字典的问题,所以我通过取出字典,删除循环,并在一个字母组成后终止程序来简化功能。同样,当输入在 lkng_glss_lttr_bot() 中时该函数工作,但是一旦我尝试将输入放入 main() 中,我就无法让程序执行。
我在 SO 上看到的示例建议在 main() 之外创建一个单独的函数来存储信息并让用户在该单独的函数中输入,但这不适用于我的任务要求。也有人建议添加全局变量。我试过了,仍然收到同样的错误。
我尝试为每个变量创建嵌套函数,并 returning 变量,但这也没有用。我认为将函数嵌套在 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()
这封信应该是这样的:
,aloL tseraeD yM
.enif m'I ?uoy 时代 woH
!uoy ssim I
,noitceffa dna evol ym lla htiW
oG-oG
!em llaC .S.P
如果输入提示在 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()
我需要创建一个函数来为 class 赋值执行以下操作: - 编写一个 Python 函数,它将接受来自用户的三个字符串值作为输入。该方法将 return 以相反顺序连接到用户的字符串值。该函数将从 main 方法中调用。 - 在 main 方法中,提示用户输入三个字符串。
最初,我创建了一个带有嵌套字典的函数 (lkng_glss_lttr_bot),它会提示用户输入(字母的各个部分),将其存储在字典中,连接条目,然后 return 反向排序的字符串作为各种 Looking Glass Letter。该函数按我预期的方式工作,我能够创建一个带有循环的小程序,以便用户可以继续添加更多条目。但是一旦我添加了 main 方法,从 lkng_glss_lttr_bot() 中删除了输入并尝试将输入放入 main() 中,一切都变得一团糟。
目前,我收到 NameError:名称 'recipient' 未定义。如果我注释掉 get_recipient() 函数,那么它将继续到下一个嵌套函数并给我一个 NameError。
我的代码中遗漏了允许命名变量的内容 - 但我是菜鸟,我不知道问题出在哪里。
我认为可能是嵌套字典的问题,所以我通过取出字典,删除循环,并在一个字母组成后终止程序来简化功能。同样,当输入在 lkng_glss_lttr_bot() 中时该函数工作,但是一旦我尝试将输入放入 main() 中,我就无法让程序执行。
我在 SO 上看到的示例建议在 main() 之外创建一个单独的函数来存储信息并让用户在该单独的函数中输入,但这不适用于我的任务要求。也有人建议添加全局变量。我试过了,仍然收到同样的错误。
我尝试为每个变量创建嵌套函数,并 returning 变量,但这也没有用。我认为将函数嵌套在 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()
这封信应该是这样的:
,aloL tseraeD yM
.enif m'I ?uoy 时代 woH
!uoy ssim I
,noitceffa dna evol ym lla htiW
oG-oG
!em llaC .S.P
如果输入提示在 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()