简体   繁体   中英

How do you get the logical NAND of two variables in Python

So, I'm continuing my Giraffe Program in Python (don't ask) and I'm making a function that makes 50 random trees in a 1000 by 1000 area.

I need to make sure that Tree 2's x and y both are not the same as Tree 1's x and y. This takes a NAND Gate. I'm okay with one of them being the same, I'm okay with neither being the same, but not with both being the same. I can't seem to find anything about making NAND Gates in python. I'm fine with defining a function to make a NAND. Can anyone help?

Since NAND is the negation of and, I would assume

not (a and b ) 

should totally work, with a and b as inputs or do I miss something?.

Interpreting:

Tree 2's x and y both are not the same as Tree 1's x and y

As:

Tree 2's x and y are not both the same as Tree 1's x and y

return (t1.x, t1.y) != (t2.x, t2.y)

Equivalently, you can also use ~(a&b)+2 , though I'm not sure why you would prefer it:

opts = [(0,0),(0,1),(1,0),(1,1)]
[print(f"{a} NAND {b} = {~(a&b)+2}") for a,b in opts]
0 NAND 0 = 1
0 NAND 1 = 1
1 NAND 0 = 1
1 NAND 1 = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM