Python 让var的布尔值跟随到另一个文件中

Python 让var的布尔值跟随到另一个文件中,python,python-3.x,variables,pycharm,text-based,Python,Python 3.x,Variables,Pycharm,Text Based,我的基于文本的游戏中有两个文件。正在分配的变量将被保留 如果输入Take,则布尔值True被指定给have_note-001 但是在下一个文件中,如果have_note_001==True,我会得到一个错误 have_注释-001未定义 If keep_note is == "Take": have_note_001 = True 然后在下一个文件中,我希望该真值传递到下一个文件 If have_note_001 == True: print("This Value Is True")

我的基于文本的游戏中有两个文件。正在分配的变量将被保留

如果输入Take,则布尔值True被指定给have_note-001 但是在下一个文件中,如果have_note_001==True,我会得到一个错误 have_注释-001未定义

If keep_note is == "Take":
    have_note_001 = True
然后在下一个文件中,我希望该真值传递到下一个文件

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 + ")")
这是我的下一个文件

from intros.intro_001 import have_note_001
if have_note_01 == True:
    print("True")
elif have_note_01 == False:
    print("False")
在文件中,导入正在工作。 我正在导入have_note_001。它只是没有传递真正的值。它似乎不记得您在第一个文件中为第二个文件指定的值


导入时,如何将变量的值分配给另一个文件?

我不确定您要求的内容是否符合您的最佳利益。默认情况下,当您导入变量所来自的文件时,存储在变量中的值已被带入。然而,这种零星的体系结构并不是真正的好做法。让我给你一些关于你的计划的反馈。首先,让我们对其进行一些输入验证:

# 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")
现在让我们谈谈你问题的主要主题。将值分配给导入时的变量结转。正如我已经提到的,这通常不是您想要的,这就是为什么大多数Python程序都有代码,包括:

if __name__ == "__main__":
    # do xyz....
这可确保仅在运行文件时才运行xyz,而在导入文件时不会运行xyz


为了更好地衡量,我建议您签出:,阅读本项目中的代码将使您更好地了解如何处理自己的项目。函数、类和继承的基础知识

我只是看到,我解释我想要什么的部分在代码部分中。对此我很抱歉。不像pyCharm,格式很好,如果不是的话,pyCharm会告诉我。我复制并粘贴到这个文件中,它说点击控制+k,所以我做了,这就是结果,你说的结转到下一个文件是什么意思?我已经更新了代码,这更好吗?我说,PyCharm中的格式说明它是正确的,当我提问时,我复制并粘贴了PyCharm中的代码到问题框中。Stack overflow说它不正确,所以我在stackoverflow内部的代码中使用了control+k。我更新了格式,这更好吗?好吧,如果不是有效的Python。大写:不,这至少意味着这篇文章需要工作。好吧,我很欣赏这个答案,我确信它是正确的,但我对Python非常陌生,我不知道这里面发生了什么。真的谢谢你的回复,只是我不知道哈哈。我不想让你解释,但我会看看link@16YearOldDev我补充了一些解释。如果您需要任何澄清,请告诉我。