为什么熊(布尔)不能保持其位置?

时间:2016-09-06 02:07:39

标签: python recursion boolean global

我想添加更多功能来练习Zed Shaw的LPTHW 35。该脚本运行时没有崩溃,并允许玩家重新访问以前的房间,如我所愿。但是,在熊市中,玩家可以通过的唯一方法是对熊进行尖叫,将bear_moved布尔值切换为True。

如果玩家这样做然后向后进入起始室,我的意图就是将bear_moved布尔值保持在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()

2 个答案:

答案 0 :(得分:2)

这是 bear_room 功能的顶部,修复了全局功能。你仍然需要适当地改变对例程的调用。

您仍在学习布尔值;请注意我在测试中所做的更改。您不必测试 bear_moved == True ;变量本身一个真/假值。将其与 True 进行比较无效。将其与错误进行比较只是操作。我删除了您的其他:传递,因为达到该位置的唯一方法是,如果bear_moved是 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."

答案 1 :(得分:0)

我对Python的全局变量和局部变量进行了更多研究。我现在意识到我正在改变函数中的局部变量,而不是全局的bear_moved。这就是为什么熊在重新进入时将自己的肥胖自我重新放回门前的原因。

从bear_room()函数中剥离参数并将其内部的bear_moved引用设置为全局后,该函数就像我原先想要的那样工作。我还用bear_moved或者不是bear_moved替换了bear_moved ==(boolean)表达式。

from sys import exit
bear_moved = False

# Exercise 35 of Zed Shaw's LPTHW
# Player can now revisit previous rooms
# The bear_room function uses simple recursion 
# instead of the while loop of the original script


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()
    else:
        print "Type a number!"
        gold_room()

def bear_room():

    """Globalizing bear_moved variable
       and stripping parameter as advised."""

    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?"
    elif bear_moved:
        print "The bear has moved away from the door."


    next = raw_input("> ")
    if 'honey' in next:
        dead("The looks at you and then slaps your face off.")
    elif 'taunt' in next and not bear_moved:
        bear_moved = True
        bear_room()
    elif 'taunt' in next and bear_moved:
        dead("The bear flies into a rage and chews your leg off.")
    elif 'open' in next and bear_moved:
        gold_room()
    elif 'open' in next and not bear_moved:
        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()

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 or 'run' in next:
        start()
    elif 'head' in next:
        dead("Well that was tasy!")
    else:
        cthulhu_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()
    elif 'right' in next:
        cthulhu_room()
    else:
        start()

start()