-3

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()
7
  • 3
    What is your question? On a side note, one obvious problem: don't compare strings ("5") and integers (len(...)). Commented Feb 11, 2018 at 15:28
  • What's the question? Also, please reformat the sample code. Commented Feb 11, 2018 at 15:30
  • when i call the script, the func1 and func2 and func3 are not working. Commented Feb 11, 2018 at 15:30
  • Possible duplicate? stackoverflow.com/questions/40403108/… Commented Feb 11, 2018 at 15:31
  • 5 != "5", as mentioned, use if len(sys.argv) == 5: and elif len(sys.argv) == 8: to correctly compare the number of arguments passed. Commented Feb 11, 2018 at 15:35

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.