2017-04-02 91 views
-3

这是我为Learn Python the Hard Way练习36编写的代码。但是我无法运行door_1函数中的代码。如果我选择3作为选项,然后向左或向右移动存储在目录中的任何内容,则无论输入什么内容,输出都是“狮子吃了你”。卡在第36次练习中学习Python困难之路

from sys import exit #importing module from lib 

def Tadaa(): 
    print "This is a place where you will get a surprise" 
    next = int(raw_input("Enter any number from 1-10 :")) 
    if next <= 10: 
     if next % 2 == 0 : 
      print "You will be buying me a shake :)." 
     else : 
      print "You will be getting a shake by me." 
    else : 
     print "Do it correctly." 

def door_1(): 
    print "There are 3 doors . Choose any door from the the remaining three doors" 
    print "Lets hope for the best " 
    next = raw_input("Enter your option :") 
    if next == "1": 
     print "abc " 
    elif next == "2": 
     print "abc" 
    elif next == "3": 
     print "You have entered 3rd door ." 
     print "Here are 2 doors one on left and one on right." 

     dir = raw_input("Choose which door do you wnat to enter :") 

     if dir == "left" or "Left": 
      print "Lion ate you . You are dead ." 
     elif dir == "right" or "Right" : 
      print "You will be getting a surprise" 
      Tadaa() 
     else : 
      print "abc" 
    else : 
     print "abc" 

def door_2(): 
    print "There are two doors A and B which will decide your fate" 

    next = raw_input("Enter the door ") 

    if next == "A" or "a": 
     door_1()    
    elif next == "B" or "b": 
     print "You are back from where you have started" 
     start() 
    else : 
     print "I got no idea what that means." 
     exit(0) 

def start(): 
    print "You are in dark room" 
    print "There is a door to your right and left ." 
    print "Which one do you take" 

    next = raw_input("> ") 

    if next == "Right" or "right": 
     door_2() 
    elif next == "Left" or "left": 
     door_1() 
    else : 
     print "abc" 
start() 

回答

2

的问题是你的声明:

if dir=="left" or "Left": 

你想要的是

if dir=="left" or dir=="Left": 

实际上,什么只是在做or "Left"是干什么的,是检查是否你已经字符串刚刚创建存在与否。换句话说,它类似于:

x='Left' 
if x: 

X确实存在,所以if XTrue

这里的关键是要始终评估量子语句,并且当您使用andor语句一起使用时,请确保使用括号是明确的。 if statement_one or statement_two

+0

但是如果你会看到start()函数,你会看到我已经对下一个语句做了同样的事情,但它确实运行了 –

+0

嗯。我不能确定,但​​我可以告诉你的一个事实就是你评估'if'陈述不正确的方式:)如上所示正确执行,看看是否能解决你的问题。 – Henry

+0

如果你了解下一条语句,请告诉我:) –

相关问题