0

I have two files in my text-based game. The variable that is being assigned will be keep_note.

If you enter "Take" the boolean value True is assigned to the have_note-001 but in the next file when if have_note_001 == True I get an error saying have_note-001 is undefined

If keep_note is == "Take":
    have_note_001 = True

Then in the next file I want that True value to travel over to the next file.

If have_note_001 == True: print("This Value Is True")            
keep_paper = input("Do you want to Leave the piece of paper or Take it? > ")
if keep_paper == "Take":
     have_note_01 = True
if have_note_01 == True:
     print("You have chosen to keep the piece of paper")
     print("You leave the house with the note(" + note_001 + ")")

This is my next file

from intros.intro_001 import have_note_001
if have_note_01 == True:
    print("True")
elif have_note_01 == False:
    print("False")

In the file, the import is working. I am importing the have_note_001. It is just not transferring the value True over. It doesnt seem to remember when you give it that value you in the first file, to the second

How can I have the value assigned to a variable carry over to another file when imported?

  • I am just seeing that the part where I explain what I want is within the code portion. I am sorry about that. – 16 Year Old Dev Sep 03 '18 at 01:08
  • Nah like on pyCharm the formatting is fine, if not pyCharm would tell me. I copied and pasted it into this and it said hit control + k so I did and this is what came out – 16 Year Old Dev Sep 03 '18 at 01:53
  • What do you mean by carry over to then next file? – Stephen Rauch Sep 03 '18 at 02:11
  • I have updated the code, is this better? I said, the formatting in PyCharm says it is correct, I copied and pasted the code from PyCharm into the question box when I asked the question. Stack overflow said it was incorrect so I used control + k on my code inside of stackoverflow. I updated the format is this better? – 16 Year Old Dev Sep 03 '18 at 02:12
  • Well `If` is not valid Python. (Capital I) So no, that at least means the post needs work. – Stephen Rauch Sep 03 '18 at 02:14
  • @StephenRauch I mean when I import have_note-001 into my next file, then have if have-note-001 == True then print("True") but I get an error saying have_note-001 is undefined, but in the first file if you input "Take" when it asks you too, it assigns the value to True. – 16 Year Old Dev Sep 03 '18 at 02:15

1 Answers1

1

I'm not sure what you are asking for is in your best interest. The values stored in variables are already carried over by default when you import the file they are from. However, this type of sporadic architecture is not really considered good practice. Let me give you some feedback on your program. First lets give it some input validation:

# start off setting keep_paper to nothing
keep_paper = ''

# As long as the player does not enter 'take' or 'leave' we are going to
# keep asking them to enter a proper response. 
while keep_paper not in ['take', 'leave']:
    # here we are going to "try" and ask the player for his choice
    try:
         # here we are getting input from the user with input(...)
         # then converting it into a string with str(...)
         # then converting it to lowercase with .lower()
         # all together str(input(...)).lower()
         keep_paper = str(input("Do you want to Leave the piece of paper or Take it? > ")).lower()
    # if the player entered an invalid response such as "53" we will go back 
    # to the beginning and ask for another response. 
    except ValueError:
        print("Sorry, I didn't understand that.")
        # ask the user to provide valid input
        continue

 if have_note_01 == True:
        print("True")
    elif have_note_01 == False:
        print("False")

Now let's address the main topic of your question. Having the value assigned to a variable carry over on imports. As I've already mentioned, this is generally not something that you want, which is why most Python programs have code including:

if __name__ == "__main__":
    # do xyz....

This ensures that xyz is only run if the file is being ran, and will not run if the file is imported.

For good measure, I recommend you checkout: https://github.com/phillipjohnson/text-adventure-tut/tree/master/adventuretutorial, reading over the code in this project will give you a better idea at how you might want to tackle your own project. (The basics of functions, classes and inheritance)

Philip DiSarro
  • 1,007
  • 6
  • 9
  • Okay so I appreciate the answer and I am sure it is right, but I am very new to Python and I have no clue on what is going on in that. Thank you for the response for real, just I have no idea lol. Im not gonna ask you to explain, but im gonna check out that link – 16 Year Old Dev Sep 03 '18 at 01:55
  • @16YearOldDev I've added some more explanation. Let me know if you need clarification on anything here. – Philip DiSarro Sep 03 '18 at 03:03