Python 如何使用函数将布尔值从False更改为True

Python 如何使用函数将布尔值从False更改为True,python,python-2.7,Python,Python 2.7,我一直在编写一个基于文本的RPG,以熟悉类、函数和模块,以及它们如何交互和相互关联。目前,我有一个房间,真的是一个功能,它的工作方式有点像一个集线器类型的区域来连接3个不同的房间。我将在这里发布一些代码,并在这段代码之后详细阐述我的问题。代码看起来有点像这样(简化以显示我的观点): 其思想是,我希望在用户运行函数时更改布尔值。这样,当他们回到枢纽式区域时,他们将不被允许重新访问某个房间。当然,这可能不是一个很好的方法来做这样的事情,如果是这样的话,我想向大家介绍一些其他的解决方案来解决这个问题,

我一直在编写一个基于文本的RPG,以熟悉类、函数和模块,以及它们如何交互和相互关联。目前,我有一个房间,真的是一个功能,它的工作方式有点像一个集线器类型的区域来连接3个不同的房间。我将在这里发布一些代码,并在这段代码之后详细阐述我的问题。代码看起来有点像这样(简化以显示我的观点):

其思想是,我希望在用户运行函数时更改布尔值。这样,当他们回到枢纽式区域时,他们将不被允许重新访问某个房间。当然,这可能不是一个很好的方法来做这样的事情,如果是这样的话,我想向大家介绍一些其他的解决方案来解决这个问题,即在球员访问某个房间后,限制他们进入该房间

我尝试将布尔值作为全局变量,并将其列在Python文件的顶部,以便hub函数可以引用布尔值来确定可用的选项。然而,在声明新的布尔值方面,这种全局布尔值方法和函数内部的局部布尔值方法似乎都不能很好地工作

有没有一种方法可以用布尔值来判断一个玩家是否完成了某个特定的功能,或者有没有更好的方法

编辑: 下面是我引用的代码的简图。我移动了布尔值以在玩家输入[D]后更改值;然而,它仍然循环通过并允许他们回到房间

def area_1_c(self):
    global dungeon_area_1
    dungeon = dungeon_area_1()
    print "You step into a hub like area."
    print "There are three ways to go: "
    print "North, east and west. "
    print ' '
    print "You can now also view your inventory anytime by pressing [I] when prompted for a choice. "
    print ' '
    print "Press [W] for North, [D] for East, [A] for West, [I] to view inventory."
    input = raw_input("What is your next choice?: ").lower()
    while input:
        if input == 'w':
            dungeon.area_1_f()
            print " "

        elif input == 'd':
            bool = False
            if bool == True:
                print "You have already explored this area. "
                print ' '
                input = raw_input("What is your next choice?: ").lower()
                continue
            else:
                bool = True
                print "You walk towards the east. "
                print ' '
                dungeon.area_1_d()

        elif input == 'a':
            dungeon.area_1_e()

        elif input == 'i':
            print inventory
            print ' '
            input = raw_input("What is your choice?: ").lower()
            continue    

        else:
            return

def area_1_d(self):
    print "You push open an iron door, behind it lies a room decorated in vines and insects."
    print "Out of the corner of your eye, you notice a chest.  You walk towards it. "
    input = raw_input("Press [Y] to open chest.: ").lower()
    if input == 'y':
        x = random.randint(1, 20)
        global dungeon_area_1
        if x >= 10:
            print "You open the chest and reveal 20 gold!"
            inventory["Gold"] = (inventory["Gold"] + 20)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()

        elif x < 10:
            print "You open the chest and reveal 5 gold!"
            inventory["Gold"] = (inventory["Gold"] + 5)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()
def区域(自身):
全球地下城区域1
地下城=地下城\区域\ 1()
打印“您进入一个轮毂状区域。”
打印“有三条路要走:”
打印“北、东、西”
打印“
print“您现在还可以在提示选择时按[I]随时查看您的库存。”
打印“
打印“北按[W],东按[D],西按[A],查看库存。”
输入=原始输入(“您的下一个选择是什么:”).lower()
输入时:
如果输入='w':
地牢。区域(1)
打印“”
elif输入=='d':
bool=False
如果bool==True:
打印“您已经探索过该区域。”
打印“
输入=原始输入(“您的下一个选择是什么:”).lower()
持续
其他:
布尔=真
打印“你向东走。”
打印“
地牢。区域_1_d()
elif输入==“a”:
地牢。区域(1)
elif输入==“i”:
打印库存
打印“
输入=原始输入(“您的选择是什么?”)。下限()
继续
其他:
返回
def区域_1_d(自身):
打印“你推开一扇铁门,门后是一间用藤蔓和昆虫装饰的房间。”
打印“从你的眼角,你注意到一个胸部。你朝它走去。”
输入=原始输入(“按[Y]打开胸部”)。下()
如果输入='y':
x=random.randint(1,20)
全球地下城区域1
如果x>=10:
打印“打开箱子,露出20块金子!”
存货[“黄金”]=(存货[“黄金”]+20)
打印“你转身走出门,回到中心区域。”
地下城=地下城\区域\ 1()
地牢。区域_1_c()
elif x<10:
打印“打开箱子,露出5块金子!”
存货[“黄金”]=(存货[“黄金”]+5)
打印“你转身走出门,回到中心区域。”
地下城=地下城\区域\ 1()
地牢。区域_1_c()

对不起,如果代码很混乱,什么都没有。它离完成还有很长的路要走。

我解决了它。谢谢你的帮助,尤其是上面的汤姆·卡兹。我将布尔变量更改为全局变量,然后注意到
global bool
,我可以根据用户选择的[D]更改全局变量,方法是:

elif输入=='d':
全球布尔
bool=True


它现在按我希望的方式工作。

我解决了它。谢谢你的帮助,尤其是上面的汤姆·卡兹。我将布尔变量更改为全局变量,然后注意到
global bool
,我可以根据用户选择的[D]更改全局变量,方法是:

elif输入=='d':
全球布尔
bool=True


它现在按照我希望的方式工作。

为什么不在调用room函数后在
room\u hub
中设置值呢?例如,在
if room\u e\u bool:room\u e\u bool=True
之后(另外,您不需要与True/False进行比较,只要执行
if variable
)。或者只需room\u e\u bool=!e室_bool@EL3PHANTEN嗯,不。这将允许他们在第二次尝试进入房间时返回房间。这将是使用类的完美示例:)为什么不在调用房间函数后在
room\u hub
中设置值?例如,在
之后如果room\u bool:room\u e\u bool=True
(另外,您不需要比较真/假,只要做
if variable
)即可。或者只需room_e_bool=!room_e_bool@EL3PHANTEN哦,不。这将允许他们在第二次尝试进入房间时返回房间。这将是使用类的完美示例:)
def area_1_c(self):
    global dungeon_area_1
    dungeon = dungeon_area_1()
    print "You step into a hub like area."
    print "There are three ways to go: "
    print "North, east and west. "
    print ' '
    print "You can now also view your inventory anytime by pressing [I] when prompted for a choice. "
    print ' '
    print "Press [W] for North, [D] for East, [A] for West, [I] to view inventory."
    input = raw_input("What is your next choice?: ").lower()
    while input:
        if input == 'w':
            dungeon.area_1_f()
            print " "

        elif input == 'd':
            bool = False
            if bool == True:
                print "You have already explored this area. "
                print ' '
                input = raw_input("What is your next choice?: ").lower()
                continue
            else:
                bool = True
                print "You walk towards the east. "
                print ' '
                dungeon.area_1_d()

        elif input == 'a':
            dungeon.area_1_e()

        elif input == 'i':
            print inventory
            print ' '
            input = raw_input("What is your choice?: ").lower()
            continue    

        else:
            return

def area_1_d(self):
    print "You push open an iron door, behind it lies a room decorated in vines and insects."
    print "Out of the corner of your eye, you notice a chest.  You walk towards it. "
    input = raw_input("Press [Y] to open chest.: ").lower()
    if input == 'y':
        x = random.randint(1, 20)
        global dungeon_area_1
        if x >= 10:
            print "You open the chest and reveal 20 gold!"
            inventory["Gold"] = (inventory["Gold"] + 20)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()

        elif x < 10:
            print "You open the chest and reveal 5 gold!"
            inventory["Gold"] = (inventory["Gold"] + 5)
            print "You turn around and walk out the door, back to the hub area. "
            dungeon = dungeon_area_1()
            dungeon.area_1_c()