Python 为什么';熊(布尔值)是否保持在其位置?

Python 为什么';熊(布尔值)是否保持在其位置?,python,recursion,boolean,global,Python,Recursion,Boolean,Global,我想在Zed Shaw的LPTHW练习35中添加更多功能。脚本运行时不会崩溃,并允许玩家按照我的要求重新访问以前的房间。然而,在熊室中,玩家唯一能通过的方法是对着熊尖叫,将熊移动布尔值切换为True 如果玩家这样做,然后返回到开始室,我的意图是熊仍然保持在真实的位置,这意味着熊在重新进入时仍然会离开门 当我运行脚本时,这不会发生;当我第一次进入熊室时,我对熊尖叫,导致它离开门。然后我离开房间,回到起始房间。当我回到熊的房间时,熊又神秘地扑通一声把它肥胖的自己关在了门前 我把bear_moved布

我想在Zed Shaw的LPTHW练习35中添加更多功能。脚本运行时不会崩溃,并允许玩家按照我的要求重新访问以前的房间。然而,在熊室中,玩家唯一能通过的方法是对着熊尖叫,将熊移动布尔值切换为True

如果玩家这样做,然后返回到开始室,我的意图是熊仍然保持在真实的位置,这意味着熊在重新进入时仍然会离开门

当我运行脚本时,这不会发生;当我第一次进入熊室时,我对熊尖叫,导致它离开门。然后我离开房间,回到起始房间。当我回到熊的房间时,熊又神秘地扑通一声把它肥胖的自己关在了门前

我把bear_moved布尔值放在函数外就是为了这个目的——这是我唯一能想到的给程序提供额外功能的方法

总而言之,当我离开熊屋并重新进入时,熊为什么不动呢?如何实现我的目标功能

from sys import exit
bear_moved = False

def gold_room():
    print "This room is full of gold. How much do you take?"
    next = raw_input("> ")
    if next.isdigit():
        if int(next) > 101:
            dead("You greedy bastard!")
        elif int(next) < 101:
            print "Nice, you're not greedy! You win!"
            exit(0)
        else:
            pass
    elif 'back' in next or 'backtrack' in next:
        bear_room(bear_moved)
    else:
        print "Type a number!"
        gold_room()

def bear_room(bear_moved):
    if bear_moved == False:
        print "There's a bear in here."
        print "The bear has a bunch of honey."
        print "The fat bear is in front of another door."
        print "How are you going to move the bear?"
    elif bear_moved == True:
        print "The bear has moved away from the door."
    else:
        pass

    next = raw_input("> ")
    if 'honey' in next:
        dead("The looks at you and then slaps your face off.")
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == False:
        bear_moved = True
        bear_room(bear_moved)
    elif 'taunt' in next or 'tease' in next or 'scream' in next or 'yell' in next and bear_moved == True:
        dead("The bear flies into a rage and chews your leg off.")
    elif 'open' in next and bear_moved == True:
        gold_room()
    elif 'open' in next and bear_moved == False:
        dead("The bear, still standing in the way, lunges up and rips your throat out.")
    elif 'back' in next or 'backtrack' in next:
        start()
    else:
        print "I got no idea what this means."
        bear_room(bear_moved)

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane."
    print "Do you flee for your life or eat your head?"

    next = raw_input("> ").lower()

    if 'flee' in next:
        start()
    elif 'head' in next:
        dead("Well that was tasy!")
    else:
        cthuhlu_room()

def dead(why):
    print why, "Game Over!"
    exit(0)

def start():
    print "You're in a dark room."
    print "You have no idea who you are or how you got here."
    print "Your head is throbbing..."
    print "There are two doors, one to your left, and one to your right."
    print "Which door do you take?"

    next = raw_input("> ").lower()

    if 'left' in next:
        bear_room(bear_moved)
    elif 'right' in next:
        cthulhu_room()
    else:
        start()

start()
从系统导入退出
bear_moved=假
def gold_房间():
打印“这间屋子里满是金子,你要多少钱?”
下一步=原始输入(“>”)
如果next.isdigit():
如果int(next)>101:
死了(“你这个贪婪的混蛋!”)
elif int(下一个)<101:
打印“很好,你不贪婪!你赢了!”
出口(0)
其他:
通过
elif“后退”在下一步或“后退”在下一步:
熊屋(熊屋已搬迁)
其他:
打印“键入一个数字!”
黄金屋
def bear_房间(bear_已移动):
如果bear_moved==False:
打印“这里有一只熊。”
打印“熊有一束蜂蜜。”
打印“肥熊在另一扇门前。”
打印“你打算如何移动熊?”
elif bear_moved==真:
打印“熊已经离开了门。”
其他:
通过
下一步=原始输入(“>”)
如果“蜂蜜”出现在下一个:
死了(“他看着你,然后拍了拍你的脸。”)
elif在next中的“嘲弄”或在next中的“戏弄”或在next中的“尖叫”或在next中的“喊叫”和bear_moved==False:
bear_moved=True
熊屋(熊屋已搬迁)
elif在next中的“嘲弄”或在next中的“戏弄”或在next中的“尖叫”或在next中的“喊叫”和bear_moved==真:
死了(“熊勃然大怒,咬掉你的腿。”)
elif“open”在next和bear_moved==True:
黄金屋
elif“open”在next和bear_moved==False:
死了(“熊,仍然站在路上,猛冲过来,撕开你的喉咙。”)
elif“后退”在下一步或“后退”在下一步:
开始()
其他:
打印“我不知道这意味着什么。”
熊屋(熊屋已搬迁)
def CTHULU_房间():
打印“这里你看到了邪恶的克图鲁。”
打印“他,它,任何盯着你看的东西,你都会发疯。”
打印“你是逃命还是吃脑袋?”
next=原始输入(“>”).lower()
如果在下一步中“逃离”:
开始()
下一步中的elif“head”:
死了(“那是塔西!”)
其他:
cthuhlu_室()
def死亡(原因):
打印为什么,“游戏结束!”
出口(0)
def start():
打印“你在一个黑暗的房间里。”
打印“你不知道你是谁,也不知道你是如何来到这里的。”
打印“你的头在跳动…”
打印“有两个门,一个在你的左边,一个在你的右边。”
打印“你选哪扇门?”
next=原始输入(“>”).lower()
如果在下一步中为“左”:
熊屋(熊屋已搬迁)
在下一页中选择“右”:
cthulhu_室()
其他:
开始()
开始()

这是您的熊屋功能的顶部,全局功能已修复。您仍然必须适当地更改对例程的调用

您仍在学习布尔值;请注意我在测试中所做的更改。你不必测试bear_moved==True;变量本身是真/假值。将其与True进行比较没有任何作用。将其与False进行比较只不过是not操作。我删除了你的else:pass,因为到达该位置的唯一方法是如果bear_移动的是NaN(不是一个数字),这是一个你不应该在这个程序中看到的值。从逻辑上讲,你根本无法理解那个子句

当然,您的程序还有一些改进:修复拼写和语法错误,使逻辑流程更清晰(是否为此制作了任何流程图?),嵌套if语句以节省工作和阅读时间

def bear_room():
    global bear_moved
    if not bear_moved:
        print "There's a bear in here."
        print "The bear has a bunch of honey."
        print "The fat bear is in front of another door."
        print "How are you going to move the bear?"
    else:
        print "The bear has moved away from the door."

这是您的熊屋功能的顶部,全局功能已修复。您仍然必须适当地更改对例程的调用

您仍在学习布尔值;请注意我在测试中所做的更改。你不必测试bear_moved==True;变量本身是真/假值。将其与True进行比较没有任何作用。将其与False进行比较只不过是not操作。我删除了你的else:pass,因为到达该位置的唯一方法是如果bear_移动的是NaN(不是一个数字),这是一个你不应该在这个程序中看到的值。从逻辑上讲,你根本无法理解那个子句

当然,您的程序还有一些改进:修复拼写和语法错误,使逻辑流程更清晰(是否为此制作了任何流程图?),嵌套if语句以节省工作和阅读时间

def bear_room():
    global bear_moved
    if not bear_moved:
        print "There's a bear in here."
        print "The bear has a bunch of honey."
        print "The fat bear is in front of another door."
        print "How are you going to move the bear?"
    else:
        print "The bear has moved away from the door."

我进一步研究了Python的全局变量和局部变量。我现在意识到我是在函数中更改局部变量,而不是全局变量。这正是熊扑通一声扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通扑通