0
def Forest(Health,Hunger):
    print'You wake up in the middle of the forest'
    Inventory = 'Inventory: '
    Squirrel =  'Squirrel'
    while True:
        Choice1 = raw_input('You...\n')
        if Choice1 == 'Life' or 'life':
            print('Health: '+str(Health))
            print('Hunger: '+str(Hunger))
        elif Choice1 == 'Look' or 'look':
            print 'You see many trees, and what looks like an edible dead Squirrel, \na waterfall to the north and a village to the south.'
        elif Choice1 == 'Pickup' or 'pickup':
            p1 = raw_input('Pickup what?\n')
            if p1 == Squirrel:
                if Inventory == 'Inventory: ':
                    print'You picked up a Squirrel!'
                    Inventory = Inventory + Squirrel + ', '
                elif Inventory == 'Inventory: Squirrel, ':
                        print'You already picked that up!'
            else:
                print"You can't find a "+str(p1)+"."
        elif Choice1 == 'Inventory' or 'inventory':
            print Inventory

I am trying to make it so when it says You... you can type either Life, Pickup, Look, or Inventory. I have way more code on this program I am just showing you a portion. But every time I run it, it always shows the "Life" portion even if you type "Pickup" or "Look" or "Inventory". Please help! Thanks, John

EDIT: I think it's just a spacing issue but I am not sure it was running fine earlier...

Kijewski
  • 25,517
  • 12
  • 101
  • 143
John Doe Smith
  • 165
  • 1
  • 1
  • 8

3 Answers3

9

You are misunderstanding the or expression. Use this instead:

if Choice1.lower() == 'life':

or, if you must test against multiple options, use in:

if Choice1 in ('Life', 'life'):

or, if you must use or then use it like this:

if Choice1 == 'Life' or Choice1 == 'life':

and expand this to your other Choice1 tests.

Choice1 == 'Life' or 'life' is interpreted as (Choice1 == 'Life') or ('life'), with the latter part always being True. Even if it was interpreted as Choice1 == ('Life' or 'life') then the latter part would evaluate to 'Life' only (it being True as far as boolean tests go), so you'd be testing if Choice1 == 'Life' instead, and setting Choice to 'life' would never make the test pass.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I just changed everything to elif Choice1 in ('Help', 'help): and it worked perfectly Thanks! – John Doe Smith Mar 12 '13 at 22:12
  • I just did some timings. `a in (b, c)` is two to three times faster than `a.lower() == c`, and about 30 % faster than `a == b or a == c`. (tested on Python 3.3.0) – Tim Pietzcker Mar 12 '13 at 22:29
  • 1
    @TimPietzcker interesting, though for free form input the lower() is probably better as it would catch 'LIfe' or similiar variants. For completeness, I would probably add a strip() as well, e.g. Choice1.strip().lower() – ernie Mar 12 '13 at 22:34
3

You have:

    if Choice1 == 'Life' or 'life':

Which is actually the equivalent of:

    if (Choice1 == 'Life') or 'life':

A non-empty/non-zero string ('life') will always be treated as true, hence why you end up there.

You either want:

    if Choice1 == 'Life' or Choice1 == 'life':

or:

    if Choice1.lower() == 'life':
ernie
  • 6,356
  • 23
  • 28
1

Use in:

elif Choice1 in ('Pickup', 'pickup'):

Alternatively, you can use regular expressions:

import re

elif re.match("[Pp]ickup", Choice1):

Separately, I'd use a set for your inventory:

Inventory = set()
Squirrel =  'Squirrel'
while True:
...
        if p1 == Squirrel:
            if not Inventory:
                print'You picked up a Squirrel!'
                Inventory.add(Squirrel)
            elif Squirrel in Inventory:
                print'You already picked that up!'
hughdbrown
  • 47,733
  • 20
  • 85
  • 108