2015-10-15 133 views
0

我在自学python,学习Python很困难,我遇到了一个问题ex36这个if语句有什么问题

我处于开发的相当早期阶段,我无法弄清楚我的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

重要的是,这里的电脑是愚蠢的。如果我说'如果VAR == 1或2',那么计算机会检查'if VAR == 1'。然后它检查以查看“是否2”。因为'2'是一个正整数,所以'或2'将导致条件始终为真,无论“VAR”如何。你的情况是相似的,因为英文不完全符合你想要说的Python的等价物。 – turbulencetoo

+0

所以'如果var === 1或var === 2'是正确的,或者你需要检查vars内容列表 – Steve

回答

1

让我们看看这个行:

elif "1" or "2" in choice and not key: 

什么该行实际上指出的是,它基本上需要以下两个条件之一是True

  1. 如果 “1”(没有别的)中选择
  2. 如果 “2”,而不是关键

这是一个典型的错误,如果你是一个初学者,如果你写的,你可以很容易地解决这个问题如下(最简单的解决):

elif choice in [1, 2] and not key: 

这意味着:如果选择是等于列表[1,2]中包含的任何元素,并且键不为真

+0

所以我的x语句将是 'x =在[“1”,“2”]中没有选择,而不是关键和狮子 如何格式化代码在评论中? – Steve

+0

是的,这是正确的。把你的代码放在这两个符号之间。 – nikaltipar

-3
elif any(x in ["1", "2"] for x in choice) and not key: 
+0

这是什么意思? – Steve

+0

这意味着如果“1”或“2”显示为选择的子字符串。你的第一条if语句是可行的,因为它被评估为'如果为真且选择键',因为非空字符串评估为'真' –

+0

这是逻辑语句的正确格式;我是否需要做这样的事情,即使我只想检查用户输入是否等于一个字符串? – Steve

3
elif "1" or "2" in choice and not key 

这interpretted如下( “1” 或(( “2” 中选择)和(非键)))

由于 “1” 总是为真,这总是如此。我想你的意思是:

elif choice in ['1', '2'] and not key 
+0

为什么它在第一个if语句中工作呢? – Steve

+0

哪一个是第一个?如果你的意思是“如果”在选择中采取“和”键“:',这也是错误的。 “take”总是如此“选择”中的“key”有时是真的。当它们都是真的时,那么这个表达是真实的。你也应该在[“take”,“key”]中有选择。 – RobertB

+0

那么如何构造这些语句,以便python以他想要的方式来解释它们?有没有一个资源解释了python的veiws代码,这样我可以在将来避免这个错误? – Steve