{Python} Is there a way to “extract” the return value variable from a function and use it elsewhere, without calling the original function?

first time posting here. Relatively new to python and I’m developing a program that’s basically a random story generator for the purpose of learning the language, develop skills and add to my portfolio.

I have a main file (initially starting the program), an intro file (for the introduction portion, getting the user’s name and where I’m trying to “extract” the user_name variable), a functions file which houses numerous functions for events I can use over and over again, like time.sleep and “YOU DIED” and then 5 “story content” files which get called by the main file based on a randomly generated number from 1-5.

What I’m trying to do is take the user_name variable from the intro file and be able to use that variable in the story content files without calling the intro function itself. Just using the returned variable from the intro function in the intro file.

Here’s the output of the part I want to use the user’s name for:
Program output

And here’s the corresponding code:
Code in question

def left_hallway_survival_success():
    from intro import intro
    user_name = intro()
    # Successful hidden passage code for hidden passage random event function    
    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")
    # Make the name of the sign say the user's name somehow
    functions.time_sleep(1, user_name + "n")
    #print ("User name - " + intro.intro() + " - from intro file function in main")


    functions.time_sleep(3, "1845-1862n")
    functions.time_sleep(4, "From the looks of the tomb, he seemed to be a wealthy man...n")

Here’s the intro file code:

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 value from function
    return user_name

I tried adding

from intro import intro
user_name = intro()

functions.time_sleep(1, "the user's name is: " + user_name)

Which resulted in the intro function being called in the middle of the story. I also tried adding the user_name variable as a parameter in the left_hallway_survival_success function, along with adding

user_name = intro()

At the top of the file where the imports are before the functions but the same thing happened, along with “user_name not defined” errors.

I hope this makes sense enough for what I’m trying to do.

Thanks in advance for your help.

New contributor

Justin Freed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật