[英]Calling functions based on a if condition in python
我必须根据传递的参数数量调用主函数。 当我调用脚本时,函数不起作用。
示例代码:
def func1():
somecode
def func2():
somecode
def func3():
somecode
def main():
if len(sys.argv) == "5":
func1()
func3()
elif len(sys.argv) == "8":
func2()
func3()
if __name__ == '__main__':
main()
在您的代码中,您将len(sys.argv)
与字符串进行比较:
if len(sys.argv) == "5":
func1()
func3()
elif len(sys.argv) == "8":
func2()
func3()
更改为
if len(sys.argv) == 5:
func1()
func3()
elif len(sys.argv) == 8:
func2()
func3()
应该做的伎俩
您的代码没有调用这些函数,因为if
-test:
if len(sys.argv) == "5":
总是假的。 函数len()
返回一个整数,Python 中的整数永远不等于字符串。 改为这样做:
if len(sys.argv) == 5:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.