Python:打印函数的返回值

Cro*_*015 2 python python-2.7

Python 2.7.以下函数返回True或False.我正在尝试打印此结果.现在我知道我可以用"print"替换"return",但我不想在函数内打印.

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()
Run Code Online (Sandbox Code Playgroud)

当我运行下面的代码时,我被提示输入a和b的值,然后输出为"None",而不是True或False.如何打印函数OR_Gate的返回值?

Pad*_*ham 5

您正在将布尔值与字符串 进行比较True != "True",False != "False"因此None当您未指定返回值时,函数将返回默认值.您还可以使用以下in方法简化代码"True":

def OR_Gate():                                              
  a = raw_input("Input A:")                                        
  b = raw_input("Input B:")                                        
  return "True" in [a,b]
Run Code Online (Sandbox Code Playgroud)