我正在艰难地学习Python和学习Python,并且在 ex36 上遇到了问题。
我处于开发的早期阶段,我无法弄清楚我的if语句有什么问题。无论出于何种原因,我的代码永远不会过去
elif "1" or "2" in choice and not key.
即使声明中没有“1”或“2”。我不明白为什么会这样。看起来很好。当我使用另一个嵌套if语句时,嵌套语句超过了这一点,但是它挂起了另一个点,所以我移动了我的初始化变量 - 不确定这是否是python中的东西 - 我确实移动了它们虽然 - 在while循环之外。 在我漫游之前,这里是完整的代码。 我知道逻辑不完整,超过一半的代码没有完成,但我需要知道为什么这个语句不起作用。
#write function definition statements.
def darkRoom():
door = raw_input(">>> ")
if "1" in door:
lions()
elif "2" in door:
tiger()
elif "3" in door:
bear()
else:
print """A thunderous voice booms through the room exclaiming,
"CHOOSE DOOR 1, 2, OR 3!"""
darkRoom()
def lions():
#naming error somewhere here
keys = False
lions = False #lions are calm if false. They are pissed if true
warning = True
while True:
choice = raw_input(">>> ")
print " %r %r %r" % (keys, lions, warning)
x = "1" or "2" not in choice and not key and lions
if "take" and "key" in choice:
key = True
print """There are two doors behind the angry pride of lions.
Which door are you going to run to and open before the lions eat you?"""
door = raw_input(">>> ")
if "1" in door and key == True:
threeBrickRoads()
elif "2" in door and key == True:
quickSand()
else:
youDie("You take too long to decide which door to take and the lions eat you.")
elif "look" in choice:
print "Looks like you're going to have to take the key from the lions"
#never gets past this statement even when 1 and two not in choice. This is what my question
#is about
elif "1" or "2" in choice and not key:
print "The Door is locked and the lions are starting to stare."
lions = True
print " %r %r %r" % (keys, lions, warning)
print "%r" % choice
#never reaches this point. I don't know why.
elif x and warning:
print """The lions leave the key and start to chase you. Quick get the
key before they catch you"""
warning = False
#Statement never reaches this point. It should
elif x and not warning:
youDie("You take too long to take the key and the lions eat you for it.")
# entering jig in statement should put me here and not at line 46
else:
print """"You quickly realize that doesn't do you any good.
You take another look at your surroundings"""
#don't think I need that while I have the while loop.
#lions()
##def tiger():
##def bear():
##def threeBrickRoads():
##def quickSand():
##def sizePuzzle():
##def riddlesOnWall():
##def wolfSheepCabbage():
##def duckHunt():
##def hangman():
##def goldRoom():
##def oceanShore():
##def winScreen():
def youDie():
print why, """You lay there pondering your mistake as
the last fleeting pulses of life slowly beat out of you."""
#exit(0)
darkRoom()
答案 0 :(得分:3)
elif "1" or "2" in choice and not key
这解释如下(“1”或((选择中的“2”)和(不是键)))
由于“1”始终为真,因此总是如此。我想你的意思是:
elif choice in ['1', '2'] and not key
答案 1 :(得分:1)
让我们来看看这一行:
elif "1" or "2" in choice and not key:
这一行实际上表明它基本上要求以下两个条件之一为True
:
如果您是初学者,这是一个典型的错误,如果您按照以下方式编写,则可以轻松解决此问题(最简单的修复):
elif choice in [1, 2] and not key:
这意味着:如果选择等于列表中包含的任何元素[1,2]且密钥不是真的。
答案 2 :(得分:-3)
None