User avatar
tewe
Posts: 37
Joined: Tue Apr 22, 2014 2:49 pm

How to use "or"

Fri Oct 24, 2014 7:40 pm

Hi!
I'm not a total beginner in python, I know the basics if you know what I mean. Right now I am working on a text-base adventure game. I want there to be two ways of typing the decision in.

Code: Select all

while True:
                answer = raw_input("You have to decide. Do you want to continue, or not")
                answer = answer.lower()

                if answer == ("continue") or ("i want to continue"):
                        game()
                        break
Now the game accepts all inputs. It doesn't have to be one of them. It even accepts "bajskorv". It works while there is only one alternative. "continue" or "i want to continue".

I don't understand what is wrong. By the way, I am using Python 2.7.3.
I really appreciate your help!
I'm a swedish viking with big beard, I like to eat raspberry pi.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to use "or"

Fri Oct 24, 2014 7:43 pm

Code: Select all

if (answer == "valid answer#1") or (answer == "valid answer#2"):
    do_something_positive()
else:
    do_something_else()
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
tewe
Posts: 37
Joined: Tue Apr 22, 2014 2:49 pm

Re: How to use "or"

Fri Oct 24, 2014 7:46 pm

DougieLawson wrote:

Code: Select all

if (answer == "valid answer#1") or (answer == "valid answer#2"):
    do_something_positive()
else:
    do_something_else()
I don't really understand what you mean. But if you mean that I need an else statement you're wrong. There is an else statement but I didn't include it in the part of code I pasted in.
I'm a swedish viking with big beard, I like to eat raspberry pi.

User avatar
DougieLawson
Posts: 40486
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to use "or"

Fri Oct 24, 2014 7:53 pm

You need to test variable against value on both sides of the OR statement.

Code: Select all

if (variable == "value1") OR (variable == "value2"):
    do_something_for_a_positive_compare()
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
tewe
Posts: 37
Joined: Tue Apr 22, 2014 2:49 pm

Re: How to use "or"

Fri Oct 24, 2014 7:56 pm

DougieLawson wrote:You need to test variable against value on both sides of the OR statement.

Code: Select all

if (variable == "value1") OR (variable == "value"):
    do_something_for_a_positive_compare()
Sorry for being a bit rude. Thanks Doug! I thinks this is the third time you fix my problems.

Thank you!
I'm a swedish viking with big beard, I like to eat raspberry pi.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How to use "or"

Sat Oct 25, 2014 1:06 pm

For anyone else reading this, I think it may be useful to explain why Dougie's answer is correct.

If you look at the docs for Boolean operations, there's an example that says:
Operation: x or y
Result: if x is false, then y, else x
This means you have the following possibilities:

Code: Select all

True or True -> True
True or False -> True
False or True -> True
False or False -> False
In the code example above, the test is evaluated as follows:

Code: Select all

#Assuming answer is something other than "continue"
answer == ("continue") or ("i want to continue")
#is the same as:
False or ("i want to continue")
#is the same as
False or True
#is the same as
True
This part of the docs explains why the string "I want to continue" is the same as True.

Dougie's amendments mean that, instead of returning True for the string, python compares answer to "I want to continue" and evaluates it as True or False as appropriate.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

billio
Posts: 71
Joined: Thu Dec 15, 2011 8:25 am
Contact: Website

Re: How to use "or"

Sat Oct 25, 2014 6:56 pm

Just to indicate an alternative way of doing this avoiding the "or" :

Code: Select all

if answer in ["continue","i want to continue"] :
    # do whatever you want to do
or in a different situation :

Code: Select all

if answer not in ["quit","stop","halt","bye"] :
    # carry on

Return to “Python”