Ex 27 - Truth Tables

I apologize this is very long and repetitive; 160 lines of code, but if it helps anyone learn the truth tables that would be good.

print("We Will Learn Truth Tables: ")

x = not True
y = not False

a = eval(input("Not True: "))
b = eval(input("Not False: "))

c = 'Correct!'
d = 'Wrong!'

if a == x:
	print(c)
else:
	print(d)
	print(x)

if b == y:
	print(c)
else:
	print(d)
	print(y)


or1 = True or False
or2 = True or True
or3 = False or True
or4 = False or False

or_1 = eval(input("True or False: "))
or_2 = eval(input("True or True: "))
or_3 = eval(input("False or True: "))
or_4 = eval(input("False or False: "))

if or_1 == or1:
	print(c)
else:
	print(d)
	print(or1)

if or_2 == or2:
	print(c)
else:
	print(d)
	print(or2)

if or_3 == or3:
	print(c)
else:
	print(d)
	print(or3)

if or_4 == or4:
	print(c)
else:
	print(d)
	print(or4)

and1 = True and False
and2 = True and True
and3 = False and True
and4 = False and False

and_1 = eval(input("True and False: "))
and_2 = eval(input("True and True: "))
and_3 = eval(input("False and True: "))
and_4 = eval(input("False and False: "))

if and_1 == and1:
	print(c)
else:
	print(d)
	print(and1)

if and_2 == and2:
	print(c)
else:
	print(d)
	print(and2)

if and_3 == and3:
	print(c)
else:
	print(d)
	print(and3)

if and_4 == and4:
	print(c)
else:
	print(d)
	print(and4)

not_or_1 = not(True or False)
not_or_2 = not(True or True)
not_or_3 = not(False or True)
not_or_4 = not(False or False)

not_or1 = eval(input("Not True or False: "))
not_or2 = eval(input("Not True or True: "))
not_or3 = eval(input("Not False or True: "))
not_or4 = eval(input("Not False or False: "))

if not_or1 == not_or_1:
	print(c)
else:
	print(d)
	print(nor_or_1)

if not_or2 == not_or_2:
	print(c)
else:
	print(d)
	print(not_or_2)

if not_or3 == not_or_3:
	print(c)
else:
	print(d)
	print(not_or_3)

if not_or4 == not_or_4:
	print(c)
else:
	print(d)
	print(not_or_4)

not_and_1 = not(True and False)
not_and_2 = not(True and True)
not_and_3 = not(False and True)
not_and_4 = not(False and False)

not_and1 = eval(input("Not True and False: "))
not_and2 = eval(input("Not True and True: "))
not_and3 = eval(input("Not False and True: "))
not_and4 = eval(input("Not False and False: "))

if not_and1 == not_and_1:
	print(c)
else:
	print(d)
	print(not_and_1)

if not_and2 == not_and_2:
	print(c)
else:
	print(d)
	print(not_and_2)

if not_and3 == not_and_3:
	print(c)
else:
	print(d)
	print(not_and_3)

if not_and4 == not_and_4:
	print(c)
else:
	print(d)
	print(not_and_4)
1 Like

I’ve memorized them but F and F equals F still confuses me. The sentence making part of my brain says F and F equals True.

x = (5>10 and 1>5)
print(x)

False

Very confusing. Short circuits my brain.

1 Like

Hello @Frankjr

If you look at it this way.

There is a lamp connected to a battery.
On each wire from the battery ( + and -) there is a switch. On is True. Off is False.
If both switches are off (False and False) will the lamp be on? No (= False)

2 Likes

well…nervermind what I posted. I was reading > as less-than. :no_mouth:
yikes.

1 Like

@Frankjr The way to read a lot of these so that it’s less confusing is to “short circuit” them. That’s also how the computer processes them:

If it’s and then it fails if one part is false. Doesn’t matter how many come after the and.

False and True and False and False

When you read that first “False and” you’re done. It’s false because you had one False.

True and True and True and False and True

In that one you had to go through 3 True before hitting a False, but now it’s done. It’s false. Just need one.

With or it’s the inverse: It’s true when you get even 1 True. Just need one, so this:

True or False or False or True

Read that first True and done. It’s true because you just need one. Doesn’t matter what comes after that.

False and False and False and True and False

You had to read 3 False before you hit a True, but now it’s done. That whole line is now True.

A free service run by Zed A. Shaw for learncodethehardway.org.