python实现与或门
时间: 2024-03-15 19:38:32 浏览: 38
以下是Python实现与门和或门的例子:
1. 实现与门
```python
def AND(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
s = x1*w1 + x2*w2
if s <= theta:
return 0
else:
return 1
print('0 AND 0 =', AND(0, 0))
print('0 AND 1 =', AND(0, 1))
print('1 AND 0 =', AND(1, 0))
print('1 AND 1 =', AND(1, 1))
```
2. 实现或门
```python
def OR(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.3
s = x1*w1 + x2*w2
if s <= theta:
return 0
else:
return 1
print('0 OR 0 =', OR(0, 0))
print('0 OR 1 =', OR(0, 1))
print('1 OR 0 =', OR(1, 0))
print('1 OR 1 =', OR(1, 1))
```
阅读全文