Home > OS >  how can I use a function as an argument to another function,
how can I use a function as an argument to another function,

Time:04-30

I am trying to write a simple code that displays 3 sets of greetings in accordance to what day of the week a user inputs. I am learning about functions as I am a beginner so please answer in terms of a function usage.

I tried to set a function for the greeting instructions and another as an input from the user which is passed as an argument to the first function. please tell me what's wrong with the code.

def action(user_response):
  
  greeting = "hello madam"
  greeting2 = " Good afternoon madam"
  greeting3 = "Have a good weekend"

  while user_response == "tuesday" or "monday":
   return greeting 
   if user_response == "wednesday" or "thursday": 
     return greeting2

  return greeting3
     



def user_response():
  user_input = input("what day of the week is it: ").lower() 
  print(f"{user_input}")
  return user_response()

def main():
  action(user_response)
  user_response()

main()

CodePudding user response:

You have some errors in your code, lets go step by step:

  • The condition user_response == "tuesday" or "monday" will return True because: if user_response is not equal to tuesday (which isn't, because you're comparing a function with a string), you will get "monday" because of or condition, False or something -> something. So you get if "monday", that evaluates to True, because not-empty string evaluates to True in python.
  • If you want to pass a function as argument, you need to call that function inside the called function. You're not returning any value from user_response, so you're retrieving None (but you aren't, because you're not capturing the result). You should return user_input from user_response
  • If you return a value, you have to capture and print it, otherwise, the function would exit doing nothing. I've modified your code so action print the greeting, only exiting at the weekend after printing.

The execution order would be:

  • Call action passing user_response as argument
  • Call user_response inside action and retrieve the value
  • Return a greeting based on the value retrieved from user_response
def action(user_func):
    greeting = "hello madam"
    greeting2 = "Good afternoon madam"
    greeting3 = "Have a good weekend"
    user_answer = user_func()
    while user_answer not in ('friday', 'saturday', 'sunday'):
        if user_answer in ("monday", "tuesday"):
            print(greeting)
        else:
            print(greeting2)
        user_answer = user_func()
    print(greeting3)
    return

def user_response():
  user_input = input("what day of the week is it: ").lower()
  return user_input

def main():
  action(user_response)

main()

CodePudding user response:

You would want your functions to return a string, which you could pass through as a parameter in to another function. Or pass a reference to the function for this to be called within the destination

For example, something like this for passing through the response

def action(response):
print(response)

def user_response():
  user_input = input("what day of the week is it: ").lower() 
  return user_input

def main():    
  response = user_response()
  action(response)

main()

Or Something like this passing through a reference to the function

def action(fn):
    response = fn()
    print(response)
    
def user_response():
  user_input = input("what day of the week is it: ").lower() 
  return user_input

def main():    
  action(user_response)

main()
  • Related