Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 2nd, 2012, 12:38 AM
 knutrainer
Registered User

Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
'and' does not work as I thought

Code:
```space1 = 'X'
space2 = 'O'
space3 = 'X'
if space1 and space2 and space3 == 'X':
print 'space1 and space2 and space3 all equal X'
else: print 'space1 and space2 and space3 do not all equal X'```

I would expect this code to print that it space1 2 and 3 do not equal X. How would I make this code print space1 2 and 3 do not equal x?

#2
December 2nd, 2012, 02:01 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
The grouping of your expression is:
Code:
```if ((space1) and (space2) and (space3 == 'X')):
...```
See why it didn't work?
knutrainer agrees: Great way to let me see the answer instead of just telling me

#3
December 2nd, 2012, 02:13 AM
 knutrainer
Registered User

Join Date: Jun 2005
Location: U.S A.Z.
Posts: 24
Time spent in forums: 13 h 14 m 44 sec
Reputation Power: 0
Code:
```space1 = 'X'
space2 = 'O'
space3 = 'X'
if space1 == 'X' and space2 == 'X' and space3 == 'X':
print 'space1 and space2 and space3 all equal X'
else: print 'space1 and space2 and space3 do not all equal X'```

Ok this works. Thanks for putting it into perspective for me.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > 'and' does not work as I thought