Using 'Or'

Christian Gollwitzer auriocus at gmx.de
Sat Jan 16 06:03:53 EST 2016


Am 15.01.16 um 21:24 schrieb Kitten Corner:
> Hi, I have python version 3.5.1 and I am working on a project, I'm trying
> to make it by using the 'or' sequence, I'm trying to make it do 1 thing or
> the other, here's an example:

> print('i like pie' or 'i like donuts')

> it only does the thing that's before the 'or', please help!

I think you misunderstand what "or" does. It evaluates the first 
expression, and if this is false, it evaluates the second. The empty 
string is considered false in Python, so, if you modfiy your example:

	print('' or 'i like donuts')

it'll print the second thing 'i like donuts'.

'or' does not choose randomly between both sides - if you are looking 
for that, check

 >>> import random
 >>> random.choice(('donuts','apples'))
'donuts'
 >>> random.choice(('donuts','apples'))
'apples'
 >>> random.choice(('donuts','apples'))
'donuts'
 >>>


	Christian




More information about the Python-list mailing list