【发布时间】:2015-12-25 17:40:04
【问题描述】:
Python 2.7。下面的函数返回 True 或 False。我正在尝试打印此结果。现在我知道我可以将“return”替换为“print”,但我不想从函数内部进行打印。
def OR_Gate():
a = raw_input("Input A:")
b = raw_input("Input B:")
if a == True or b == True:
return True
if a == False and b == False:
return False
print OR_Gate()
当我运行以下代码时,系统会提示我输入 a 和 b 的值,然后输出为“None”,而不是 True 或 False。如何打印函数 OR_Gate 的返回值?
【问题讨论】:
-
比较
a和b与字符串,例如if a == 'True' or b == 'True': -
a 或 b 永远不会是 True 或 False,它们是字符串
-
问题是因为如果块的计算结果都不是True,函数到达末尾并返回
None。
标签: python python-2.7