Python项目中如何提取函数返回值跨文件复用,避免重复调用原函数?
解决随机故事生成器的用户名传递问题
项目概况
开发一款用于语言学习、技能提升及补充作品集的随机故事生成器,文件结构包括:
- 主启动文件
intro.py:负责获取用户名的模块functions.py:存放通用功能的模块- 5个故事内容文件:由主文件随机调用
核心需求
故事内容文件需要使用intro()函数返回的user_name变量,但不能在故事文件中调用intro()函数(避免重复执行欢迎流程)。
现存问题
- 在故事函数内调用
intro():会在故事中途触发欢迎语和用户名输入流程,打断叙事 - 将
user_name设为函数参数但未正确传递:要么重复执行intro(),要么出现user_name not defined错误 - 在故事文件顶部调用
intro():会在程序启动时就执行欢迎流程,不符合主文件控制流程的逻辑
解决方案
核心思路是在主启动文件中仅调用一次intro()获取用户名,再将该变量作为参数传递给所有故事函数,让故事文件直接使用传入的参数即可,无需再处理用户名获取逻辑。
步骤1:修改故事函数,添加user_name参数
所有故事内容文件中的函数都需要接收user_name作为参数,移除内部调用intro()的代码:
def left_hallway_survival_success(user_name): # 移除原有的from intro import intro和user_name = intro()代码 functions.time_sleep(3, "You break into a sprint and carefully dodge each pendulum and make it to the other side.\n") functions.time_sleep(4, "As you approach the door, you step on a pressure plate and all the pendulums slowly stop swinging.\n") functions.time_sleep(4, "They lift up into the ceiling, out of sight.\n") functions.time_sleep(3, "You wonder how this is possible, but turn around and continue on.\n") functions.time_sleep(5, "Before you stands the door. You're unaware of what is on the other side.\n") functions.time_sleep(4, "You carefully grasp the handle and open it slowly. The hinged creak as you do this as the door appears to be very old.\n") functions.time_sleep(5, "Inside the door is yet another room. Quite smaller than the one you were just in.\n") functions.time_sleep(5, "In the center of the room you notice what appears to be a marble rectangle of some sort.\n") functions.time_sleep(5, "The room appears to be some sort of crypt, with a casket.\n") functions.time_sleep(3, "You open the lid.\n") functions.time_sleep(5, "As the lid slowly scraped across the top of it, you feel a cold breeze escape the casket.\n") functions.time_sleep(3, "Chills run down your spine.\n") functions.time_sleep(4, "You then look around the room and notice a sign above the casket.\n") functions.time_sleep(3, "It reads:\n") # 直接使用传入的user_name参数 functions.time_sleep(1, user_name + "\n") functions.time_sleep(3, "1845-1862\n") functions.time_sleep(4, "From the looks of the tomb, he seemed to be a wealthy man...\n")
步骤2:主启动文件控制流程,传递用户名
在主文件中仅调用一次intro()获取用户名,之后调用任何故事函数时都将该变量传递进去:
from intro import intro import functions # 根据实际情况导入所有故事函数 from story_module import left_hallway_survival_success def main(): # 仅执行一次用户名获取流程 user_name = intro() # 随机选择故事函数的逻辑(示例用固定函数演示) selected_story = left_hallway_survival_success # 调用故事函数并传入user_name selected_story(user_name) if __name__ == "__main__": main()
步骤3:保持intro.py不变
intro.py的代码无需修改,它只负责在主文件调用时返回用户名:
def intro(): import functions functions.time_sleep(2, "") functions.time_sleep(3, "Welcome to this awesome program!") functions.time_sleep(2, "This program generates a random interactive story!") functions.time_sleep(3, "Enjoy and have fun :)") user_name = input("Please enter your name: ").capitalize() print("Hello " + user_name + "!") return user_name
内容的提问来源于stack exchange,提问作者Justin Freed
相关产品推荐
相关产品推荐

