Search code examples
pythonfunctionargs

Passing less argument to function then defined in Python


If I have a function with multiple functions, let say, calculating an area or adding three numbers. The user chose 1 for calculating area and 2 for adding numbers via input

def func_calculate(numberOne, numberTwo, numberThree, userChoise):
    if userChoise == "1":
        calculate Area
        do something
    if userChoise == "2":
        calculate addition
        do something

userChoise is the input from user

If user wants to calculate area there is only two arguments to the function instead of three, if user wants to make an addition.

So, finally to the question... What is the most appropriate way to handle it?

When I call the function, when the user wants to calculate area, should I just set the numberThree variable to zero or something or is it a more "right" way do do it?

if userChoie == "1":    
    inputNumberOne = input(...
    inputNumberTwo = input(...
    inputNumberThree == 0 
    func_calculate(inputNumberOne, inputNumberTwo, inputNumberThree, userChoise)

Solution

  • If you wan't to perform multiple operations than it is good to have different functions for different operations

    choice = input("what user want's to do")
    
    if choice == 1:
         add()
    elif choice == 2: 
         multiply()
    

    and than take arguments from user for that operations like

    def add():
          num1 = input("num1")
          num2 = input("num2")
          num3 = input("num3")
          print(num1 + num2 + num3)
    

    and similarly for other operations

    But if you don't wan't to have multiple functions the you can do

    def func(choice):
        #choice is the integer which user opted
        if choice == 1:
              num1 = input("num1")
              num2 = input("num2")
              num3 = input("num3")
              print(num1 + num2 + num3)
        elif choice == 2:
              .........