School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,006 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,992 people online right now. Registration is fast and FREE... Join Now!




Swapping between 0 and 1 in Python

 

Swapping between 0 and 1 in Python

staunch

15 Sep, 2009 - 11:22 AM
Post #1

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 4

I'm trying to code a simple two-player game in Python. What i want is a simple way to switch between 0 and 1 in one line without obfuscation, something like this:

CODE
curplayer = not curplayer


Does that work in Python? Will not 0 evaluate to 1, and not 1 evaluate to 0? Is there something similar?

Thanks,
`Staunch

This post has been edited by staunch: 15 Sep, 2009 - 11:22 AM

User is offlineProfile CardPM
+Quote Post


girasquid

RE: Swapping Between 0 And 1 In Python

15 Sep, 2009 - 11:28 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
Python has an interactive shell that you can use to test things like this out:
CODE

>>> foo = 0
>>> foo
0
>>> foo = not foo
>>> foo
True
>>> foo = not foo
>>> foo
False
>>> int(foo)
0
>>> foo = not foo
>>> int(foo)
1
>>> foo = int(not foo)
>>> foo
0
>>> foo = int(not foo)
>>> foo
1


As you can see, you'll need to wrap the not in a call to int(), but then you should be fine.
User is offlineProfile CardPM
+Quote Post

staunch

RE: Swapping Between 0 And 1 In Python

15 Sep, 2009 - 11:32 AM
Post #3

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 4

QUOTE(girasquid @ 15 Sep, 2009 - 11:28 AM) *

As you can see, you'll need to wrap the not in a call to int(), but then you should be fine.


Ah, perfect! Thanks, man. I suppose i could have just called it up in terminal to check it myself... it's been a while since i've coded in Python. I forgot about these things. D:

User is offlineProfile CardPM
+Quote Post

code_m

RE: Swapping Between 0 And 1 In Python

17 Sep, 2009 - 10:28 AM
Post #4

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
It's worthy to note that any instantance of python (in the shell or executing a program), python will provide True and False

python
>>> type.mro(bool)
[<type 'bool'>, <type 'int'>, <type 'object'>]


Above I show that bool is a superset of int... So now you have much more clear syntax while it's still very easy for you biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:58AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month