Calling functions based on a if condition in python

2k views Asked by At

i have to call the main function based on the number of arguments passed. When i call the script the functions are not working.

Sample Code:

    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()
2

There are 2 answers

0
FlyingTeller On BEST ANSWER

In your code you are comparing len(sys.argv) with a string:

  if len(sys.argv) == "5":
    func1()
    func3()
  elif len(sys.argv) == "8":
   func2()
   func3()

changing to

  if len(sys.argv) == 5:
    func1()
    func3()
  elif len(sys.argv) == 8:
   func2()
   func3()

should do the trick

0
BoarGules On

Your code is not calling those functions because this if-test:

if len(sys.argv) == "5":

is always False. The function len() returns an integer and an integer in Python is never equal to a string. Do this instead:

if len(sys.argv) == 5: