r/learnpython • • Jul 27 '14

Alright, I give up. Help please?

Here is the project I am having troubles with: http://pastebin.com/JE1R9sGA

My goal was to create a text adventure where the player could go back and forth between each room at will instead of it just being one way.

The idea I had to do this was to use a boolean variable for each room and use an infinite loop with if statements to check which room was true, and then execute that room's scenario.

I didn't see anything wrong with it in theory, but it isn't working out how I expected...

For some reason, no matter the input, it seems to ALWAYS execute both if statements and I can't figure out why.

It is probably something small and silly that I'm just not seeing so any help would be appreciated!

Also, any other tips you guys have for me would be welcome as well, I'm still extremely new so I'm sure my code is horribly written and has a lot of room for improvement.

7 Upvotes

10 comments sorted by

7

u/Kaamelott Jul 27 '14

You should write:

    If choice == '2' or choice == 'door 2' or choice == 'door two':

Writing what you have means that it will execute if 'door 2' == True, which is always the case as it is a non zero length string.

By the way, I would personally prefer a list:

    door_number2 = ['2', 'door 2', 'door two']
    if choice in door_number2:
        Do something

Or a dictionary:

    dicdoor = {'room 1 door 1': ['1', 'door 1', 'door one'], 'room 1 door 2': [...]}
    if choice in dicdoor['room 1 door 2']:
        Do something

2

u/Primiticus Jul 27 '14

Thanks for the suggestions, I will definitely experiment and see what I like best.

3

u/phao Jul 27 '14

A simple tip...

room1 starts as true. Then in the first iteration of the loop the conditional which tests room1 == True (line 14) will go through and at the end of that block, room1 will be set to false. From now on, since nothing turns room1 back to true, that conditional will never pass and user input will never be read again.

Edit: As to why the two booleans room2 and room4 are being set to true, it's because your conditional is choice == '2' or 'door two' or 'door 2' in line 22. This conditional is always true-ish. For similar reasons, the conditional in line 19 is also always true-ish.

1

u/Primiticus Jul 27 '14

Thanks for commenting! ya I wasn't quite done with it yet, I just ran into this issue in the middle and stopped while I tried to figure it out...aaand now I'm here. I was going to add things later that would change it back to true.

So I guess I don't really understand, how would it always evaluate to true? If choice wasn't in the list of allowed responses I thought it would be false?

2

u/phao Jul 27 '14

Well, chance == '2' or 'door two' or 'door 2' doesn't check if chance is one of the itens in a list.

It checks for chance == '2' and sees it's false, then it goes for checking 'door two' and it's a true-ish value.

Check, in your python interactive mode, the result of that expression:

>>> chance = '1'
>>> chance == '2' or 'door two' or 'door 2'
'door two'

For the sake of a conditional, yours is as good as chance == '2' or true or true, which is as good as true.

1

u/Primiticus Jul 27 '14

Alright, I'm pretty sure I get it now. Thanks for the help!

3

u/ingolemo Jul 27 '14
if choice == '1' or 'door one' or 'door 1':

This line does not do what you think it does. First it will check to see if the expression choice == '1' is true and if it is not it will then check if the expression 'door one' is true and if that is also not true then it will check if 'door 1' is true. Notice how it only checks the choice variable for the first case. It so happens that the second expression is always true, so the code after the if statement will always run.

If you want to check if choice is one of those three things then you need to specify that every time because it is very difficult for python to guess what you want:

if choice == '1' or choice == 'door one' or choice == 'door 1':

Luckily this kind of 'check if a variable is one of many things' pattern is common enough that we have a shortcut for it that looks like this:

if choice in {'1', 'door one', 'door 1'}:

What's happening here is that we create a set full of the things we want to check for and then we ask if our value is in that set.

This structure that you have with a variable for each room is inefficient and complex. It would be an improvement to have one variable that is set to a string with the name of the current room.

current_room = 'room1'
...
if current_room == 'room1':
    print('You are in room 1.')
elif current_room == 'room2':
    print('You are in room 2.')

1

u/Primiticus Jul 27 '14

Awesome, I think I get it now. Thanks so much!

and thanks for the current_room idea, not sure how I didn't think of that.

1

u/DGO143 DGO143 Jul 27 '14

TLDR: Py checks if choice == '1'(== True) , then it checks if 'door one'(== True), and then if 'door 1'(== True). Especially 'door 1' might be dangerous as True == 1.

1

u/RivetSpawn Jul 27 '14

Try:

if choice in ['2', 'door 2', 'door two']:

Instead of:

If choice == '2' or choice == 'door 2' or choice == 'door two':

Same functionality just avoids typing choice more than once.