python one-line if语句调用函数如果为true

jus*_*ncf 6 python if-statement

我正在使用argparse.

def help():
  parser.print_help()
  sys.exit(0)

help() if (args.lock and args.unlock)
Run Code Online (Sandbox Code Playgroud)

这给了我一个语法错误.我的if语句出了什么问题?

Mar*_*ers 9

您正在使用条件表达式:true_result if condition else false_result.条件表达式需要一个else部分,因为它必须产生一个值; 即,当条件为假时,必须有一个表达式来产生该情况下的结果.

当你想要的只是一个正确的if陈述时,不要使用条件表达式:

if args.lock and args.unlock: help() 
Run Code Online (Sandbox Code Playgroud)