-3

This is the code I have written for Learn Python the Hard Way exercise 36. But I am not able to run the code in the door_1 function. If I choose 3 as an option and then left or right anything which is then stored in dir, the output is "lion ate you", no matter what I type.

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()
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Akshay Arora
  • 51
  • 1
  • 6

1 Answers1

3

The problem is your statement:

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

What you want is

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

In effect, what just doing or "Left" is doing, is checking whether a string that you've just created exists or not. Put another way, its similar to:

x='Left'
if x:

X does exist, so if X is True.

The crux here is to always evaluate statements in quantum, and when you're using and in conjunction with or statements, make sure you use brackets to be explicit. if statement_one or statement_two.

Henry
  • 1,646
  • 12
  • 28
  • But if you will see start() function you will see I have done the same with next statement as well but it does run – Akshay Arora Apr 02 '17 at 19:07
  • Hmm. I'm not sure off hand, but what I can tell you for a fact is the way you are evaluating `if` statements is not correct :) Do it properly as demonstrated above and see if that fixes your issue. – Henry Apr 02 '17 at 19:09
  • If you get to know about the next statement do let me know :) – Akshay Arora Apr 02 '17 at 19:14
  • Just, as I said, fix it all up. You should use `if next=='Right' or next=='right'`, for example. I dont know why it seems to be working fine now, but I'm sure its buggy code and its only working by a fluke. Debugging code is fine, debugging accidentally functional code is quite hard sometimes. – Henry Apr 02 '17 at 19:15