简体   繁体   English

你如何在Python中获得两个变量的逻辑NAND

[英]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.所以,我正在用 Python 继续我的 Giraffe 程序(不要问),我正在制作一个函数,可以在 1000 x 1000 的区域中生成 50 棵随机树。

I need to make sure that Tree 2's x and y both are not the same as Tree 1's x and y.我需要确保树 2 的 x 和 y 都与树 1 的 x 和 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.我似乎找不到任何关于在 python 中制作 NAND 门的信息。 I'm fine with defining a function to make a NAND.我可以定义一个函数来制作 NAND。 Can anyone help?任何人都可以帮忙吗?

Since NAND is the negation of and, I would assume由于 NAND 是和的否定,我假设

not (a and b ) 

should totally work, with a and b as inputs or do I miss something?.应该完全工作,以 a 和 b 作为输入还是我错过了什么?。

Interpreting:口译:

Tree 2's x and y both are not the same as Tree 1's x and y树 2 的 x 和 y 都与树 1 的 x 和 y 不同

As:如:

Tree 2's x and y are not both the same as Tree 1's x and y树 2 的 x 和 y 与树 1 的 x 和 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:同样,您也可以使用~(a&b)+2 ,但我不确定您为什么更喜欢它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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