This question already has an answer here:
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...
You are misunderstanding the
or
expression. Use this instead:or, if you must test against multiple options, use
in
:or, if you must use
or
then use it like this: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 asChoice1 == ('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 ifChoice1 == 'Life'
instead, and settingChoice
to'life'
would never make the test pass.You have:
Which is actually the equivalent of:
A non-empty/non-zero string ('life') will always be treated as true, hence why you end up there.
You either want:
or:
Use
in
:Alternatively, you can use regular expressions:
Separately, I'd use a
set
for your inventory: