# program to test truth tables v2
import time
import sys
x = 0
while x == 0:
x = str(raw_input("Please enter True or False..."))
x = x[0].upper()+ x[1:]
if x == "True":
x = True
elif x == "False":
x = False
else:
print 'invalid input!'
x = 0 # very difficult but this works now!
print x
y = 0
while y == 0:
y = str(raw_input("Please enter True or False again..."))
y = y[0].upper()+ y[1:]
if y == "True":
y = True
elif y == "False":
y = False
else:
print 'invalid input!'
y = 0
problem = 0
while problem == 0:
problem = input("Please enter number: \n1 for AND, 2 for OR, 3 for NAND, 4 for NOR...")
if problem == 1:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1.0)
print x, "AND", y, "is", x and y
elif problem == 2:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1)
print x, "OR", y, "is", x or y
elif problem == 3:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1)
print x, "NAND", y, "is", not(x and y) # not working for false/false or false/true
elif problem == 4:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(0.5)
print x, "NOR", y, "is", not(x or y) # not working for false/false
else:
print 'invalid input'
problem = 0
我认为这个项目一旦我测试了所有组合,我发现第一个while循环中的elif存在问题。请参阅上一节中的注释。感谢任何帮助
答案 0 :(得分:1)
你的第一个while循环中的问题是0 == False。把它翻译成翻译并看看。
答案 1 :(得分:1)
这是我的猜测。你需要把你的布尔变成字符串。例如,在第一个while loop
中,您将x设置为等于True
和False
而不是" True"并且"错误。"由于Python将布尔值True
和False
分别视为1和0,因此在False的情况下,while
循环变量(x和y)保持等于零。每当用户输入False
试一试,看看。让我知道。无论你做什么,继续练习!你会得到它。
编辑:是的,这就是问题所在。这是您的工作代码。import time
import sys
x = 0
while x == 0:
x = str(raw_input("Please enter True or False..."))
#x = x[0].upper()+ x[1:]
print "THIS IS X: {}".format(x)
if x == "True":
x = "True"
elif x == "False":
x = "False"
else:
print 'invalid input!'
x = 0 # very difficult but this works now!
print x
y = 0
while y == 0:
y = str(raw_input("Please enter True or False again..."))
y = y[0].upper()+ y[1:]
if y == "True":
y = "True"
elif y == "False":
y = "False"
else:
print 'invalid input!'
y = 0
problem = 0
while problem == 0:
problem = input("Please enter number: \n1 for AND, 2 for OR, 3 for NAND, 4 for NOR...")
if problem == 1:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1.0)
print x, "AND", y, "is", x and y
elif problem == 2:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1)
print x, "OR", y, "is", x or y
elif problem == 3:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(1)
print x, "NAND", y, "is", not(x and y) # not working for false/false or false/true
elif problem == 4:
print "I'm thinking.."
time.sleep(2.5)
print "the answer is..."
time.sleep(0.5)
print x, "NOR", y, "is", not(x or y) # not working for false/false
else:
print 'invalid input'
problem = 0
答案 2 :(得分:1)
其他海报说得对 - Python认为0和False是相同的,至少是真实的。当然,我们人类知道它们不是类型相同的东西,你可以强制Python通过使用“is”而不是“==”来区分它,所以这条线应该变成......
while y is 0: # <-- change that line there
y = str(raw_input("Please enter True or False again..."))
y = y[0].upper()+ y[1:]
这是一个“严格比较”,比较值(Python认为是相同的)和类型(False是布尔值,0是数字)