使用逻辑检查变量是否为s

2024-09-24 22:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

听起来很简单。我正在为Python教程制作一个“游戏”(Python the Hard Way,ex36)。你知道吗

如果玩家在未设置变量theObject的情况下选择“ahead”,则会提示他们首先获取一个对象,然后opening()将运行。但似乎不起作用。你知道吗

前方是()

# ahead
def ahead():
    if theObject not in globals():
        print "You need an object before you can proceed."
        opening()

    else:
        print "You walk for a while."
        print "Eventually you find a door to exit the Labrynthe with a code above it"
        print "You must translate the code using the code translator."
        print theObject
        if theObject == "map":
            print "You gotta get the code."
            dead("You walked too far to return. You die of exhaustion")
        elif theObject == "code":
            print "You say out aloud the magic open words 'open sesame!'"
            complete()
        else:
            print "Grab the code from the left room."
            opening()

我通过在游戏中调用ahead()来测试它,希望得到消息“您需要一个对象才能继续”,然后显示opening()。你知道吗

但我得到了这个:

ex36.py:64: SyntaxWarning: name 'theObject' is assigned to before global declaration
  global theObject
ex36.py:73: SyntaxWarning: name 'theObject' is assigned to before global declaration
  global theObject
You're in a Labrynthe.
There's a door on your left.
There's a door on your right.
Or you can go ahead.
> 

似乎没有打印消息,而是传递语法警告。你知道吗

为什么我的代码不只是像预期的那样说“在继续之前需要一个对象”?你知道吗


Tags: theto对象you游戏codeglobalprint
2条回答

globals()返回字典。尝试:

# ahead
def ahead():
    if 'theObject' not in globals():
        print "You need an object before you can proceed."
        opening()

    else:
        print "You walk for a while."
        print "Eventually you find a door to exit the Labrynthe with a code above it"
        print "You must translate the code using the code translator."
        print theObject
        if theObject == "map":
            print "You gotta get the code."
            dead("You walked too far to return. You die of exhaustion")
        elif theObject == "code":
            print "You say out aloud the magic open words 'open sesame!'"
            complete()
        else:
            print "Grab the code from the left room."
            opening()

既然我已经回答了你的问题,我要提醒你一句。这并不是最好的方法。避免这种情况的一种方法是,在开始时将theObject设置为None,然后,您就知道它总是可用的。你知道吗

再退一步,在函数中变异变量是非常糟糕的做法,并导致代码调试成为噩梦。你知道吗

最好确保变量存在并打开其值:

theObject = None

def ahead():
    if theObject is None:
        print "You need an object before you can proceed."
        opening()

    else:
        print "You walk for a while."
        print "Eventually you find a door to exit the Labrynthe with a code above it"
        print "You must translate the code using the code translator."
        print theObject
        if theObject == "map":
            print "You gotta get the code."
            dead("You walked too far to return. You die of exhaustion")
        elif theObject == "code":
            print "You say out aloud the magic open words 'open sesame!'"
            complete()
        else:
            print "Grab the code from the left room."
            opening()

相关问题 更多 >