简体   繁体   中英

Calling functions based on a if condition in python

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

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

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM