坚持第36次艰难学习Python的练习

时间:2017-04-02 19:02:28

标签: python python-2.7

这是我为Learn Python the Hard Way练习36编写的代码。但是我无法在door_1函数中运行代码。如果我选择3作为一个选项,然后左或右任何存储在目录中的东西,输出就是“狮子吃了你”,无论我输入什么。

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()

1 个答案:

答案 0 :(得分:3)

问题是你的陈述:

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