У меня есть основной файл (изначально запускается программы), вступительный файл (для вводной части, где я получаю имя пользователя и где я пытаюсь «извлечь» переменную user_name), файл функций, в котором хранятся многочисленные функции для событий, которые я могу использовать снова и снова, например time.sleep и «YOU DIED», а затем 5 файлов «содержания истории», которые вызываются основным файлом на основе случайно сгенерированного числа от 1 до 5.
Что я пытаюсь сделать нужно взять переменную user_name из вступительного файла и иметь возможность использовать эту переменную в файлах содержимого истории, не вызывая саму вступительную функцию. Просто используя переменную, возвращаемую функцией intro во вступительном файле.
Вот результат той части, для которой я хочу использовать имя пользователя:
Вывод программы
И вот соответствующий код:
Соответствующий код
Код: Выделить всё
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-1862\n")
functions.time_sleep(4, "From the looks of the tomb, he seemed to be a wealthy man...\n")
Код: Выделить всё
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
Код: Выделить всё
from intro import intro
user_name = intro()
functions.time_sleep(1, "the user's name is: " + user_name)
Код: Выделить всё
user_name = intro()
Подробнее здесь: https://stackoverflow.com/questions/788 ... and-use-it