'how can I use a function as an argument to another function,
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()
Solution 1:[1]
You have some errors in your code, lets go step by step:
- The condition
user_response == "tuesday" or "monday"will returnTruebecause: 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 oforcondition,False or something -> something. So you getif "monday", that evaluates toTrue, because not-empty string evaluates toTruein 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 retrievingNone(but you aren't, because you're not capturing the result). You should return user_input fromuser_response - If you
returna value, you have to capture and print it, otherwise, the function would exit doing nothing. I've modified your code soactionprint the greeting, only exiting at the weekend after printing.
The execution order would be:
- Call
actionpassinguser_responseas argument - Call
user_responseinsideactionand 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()
Solution 2:[2]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
