I do not understand why this loop will not work

Hey guys!

I am programming a game as per ex35 and I am quite enjoying myself! However I tried making a hidden room with a loop that will only allow you to enter with a word trigger… but it just doesn’t seem to work. Been scratching my head over it for the last few days, and it ignores the three elifs that have the hiddenroom variable inside of it. but will deal with the other if statements. IT’s like they don’t exist!

Anyone else can help? The particular issues are between line 57 and 68. Well that entire function really.

Hey Zensel,

I am unsure if you want to have the person input “north hiddenroom” or just “hiddenroom”. However, the problem maybe that user input is a string ‘hiddenroom’ is not a boolean which Choice is trying to test for. Hope that makes sense. Cheers tc

Hello Zensei
the ‘or’ statement requires you to be specific therefore you need to add the comparison again for each of the if elements
e.g.
if choice == “East” or choice == “east”:
elif choice == “West” or choice ==“west”:
elif choice == “North” or choice ==“north” and not hiddenroom:

am I making sense ?

great forum, my first post, thanks for giving me a reason
json

1 Like

just had a google and python supports the CASE like statement which you could use to shorten your if statements.
e.g
instead of
elif choice == “East” or choice ==“east”:
your could use .upper() function
if choice.upper() == “EAST”:

just a thought

1 Like

Hey Jason!

I find that all other if statements actually work if I put north or North… however that might be the reason the second part doesn’t work… except that doesn’t make the most of sense. lol

oh dude, you just taught me something, that will make my life much much easier. don’t have to type nearly as much!

I also just learned about regex… so I might have to enter that into my if statements so that they don’t get so mucked up and the user can muck up their sentences and still have them work.

This lil assignment is turning into a doozy, but I’m learning lots in the process. I appreciate your reply :slight_smile:

1 Like

Hey @tcratius,

I want the hiddenroom to be a boolean that is triggered by them saying keywords and then it becomes True and they can enter the hiddenroom if that makes any sense.

Zensei
not sure if you’ve solved this, re check your code, I’m not good at debugging yet so I add print statements to check program execution, todo this I added print(“roomB”) to the code to prove which if statement was being executed, if you run the program and type ‘west’ this should prove to you the program does not get there, try it

    if choice == "North" or "north":
        print("roomB")
        roomB(user)
    elif choice == "West" or "west":
        print("roomD")
        roomD(user)

json

You can keep the hiddenroom boolean to break the while loop. Maybe make a variable that has the word you want. (variable “string”) trigger_word = “slap” (Or something like that, lol)

    elif choice == trigger_word:
        print("There might be a way out of here!")
        hiddenroom2(user)
        hiddenroom = True // so it breaks the loop
or 

def hiddenroom2(user):
    print("Congratulations!")       
    hiddenroom = True 
    return hiddenroom  # so that it breaks the while loop

Haven’t tried it, hopefully this helps.

Cheers tc

Sorry, just thought of something.

    hiddenroom = False

    while not hiddenroom: // basically says "do while" not false (hiddenroom) 
                             which equals True
        // test the if statement 

I FIGURED IT OUT!

I had to remove my

“North” or “north” in my if statements! I had to remove all instances of north before it ran properly.

… why can’t I have or statements in my if statements?

1 Like

you can, best practice to put them in bracket if (choice = “n” or choice = “s”):

@tcratius

Thank you, that makes a lot of sense.

I went ahead and learned about regex, and modified the line to say:

if re.search("[Nn][oO][Rr]", choice):
roomB(user)

works perfectly :slight_smile: plus I learned!

2 Likes

@Zensei
thanks for posting your code, I’m also learning and seeing the difference between

re.search ⇒ find something anywhere in the string and return a match object.
re.match ⇒ find something at the beginning of the string and return a match object.

Hi Zensei,

This might be a little ahead of the lessons, but .lower() can be used to convert text to lowercase.

So regardless of North or north, and even NoRtH it will correctly trigger a boolean for north.

I’ll let you do the documentation research on proper usage.

Cheers

Cool, never looked into regex before, will now. cheers mate :smiley:

I don’t know if the web did this, but check your indentation level. You might also want to put a final else statement in with a print to see if some other condition is not being caught.

Also, don’t forget that if you want to have something like hiddenroom work between functions, you have to do this at the top of the functions that use it:

global hiddenroom

Then, set hiddenroom = False at the top of the file.