0

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

dinonuggie
  • 45
  • 6
  • 1
    There are many things wrong. What symptoms are you seeing? – quamrana Apr 29 '22 at 10:11
  • oh, the output basically is a loop asking the user for an input of the day and repeat – dinonuggie Apr 29 '22 at 10:12
  • You should put () on your user_response() under main. – Eddy Piedad Apr 29 '22 at 10:13
  • Note `user_response == "tuesday" or "monday"` doesn't do what you probably think: https://stackoverflow.com/q/20002503/3001761 – jonrsharpe Apr 29 '22 at 10:13
  • A loop? You have `while user_response == "tuesday" or "monday":`. This will always be True because it's seen as `while (user_response == "tuesday") or "monday":` and "monday" is a non-empty string and treated as true. The first line in the while loop is a `return` so you leave the loop. No real looping here. – Matthias Apr 29 '22 at 10:15
  • @quamrana I disagree with the duplicate close reason. He has many different issues apart from that. – Bharel Apr 30 '22 at 06:35
  • @bharel:I said as much in the first comment. Also the close reason is the first item discussed in the accepted answer. Still, feel free to vote for reopen. – quamrana Apr 30 '22 at 07:11
  • @quamrana If I vote it'll automatically reopen. Either way, like you said, if there are multiple issues I'm not sure the duplicate is a correct target, unless we add duplicates to all of them – Bharel Apr 30 '22 at 07:17

2 Answers2

1

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()
scmanjarrez
  • 407
  • 4
  • 8
  • Hello, thankyou so much for taking the time to answer. Just a question, I notice that instead of user_response you have added user_func as an arguement, why is that? – dinonuggie Apr 29 '22 at 10:47
  • 1
    Nothing special, just wanted to highlight the fact that is a function, and that it wasn't scope-shadowed from original user_response function. https://en.wikipedia.org/wiki/Variable_shadowing – scmanjarrez Apr 29 '22 at 10:50
  • so when I use the code, I chage that to user_response right? otherwise the function would be nothing since it is not defined. – dinonuggie Apr 29 '22 at 10:53
  • 1
    You misunderstood. user_func is declared as argument to action, every function passed to action will be named user_func. If you use user_response in action code and then you pass another function, that would execute user_response, indepently of the function you passed to action. However, user_func will be always the function you used as argument when calling action – scmanjarrez Apr 29 '22 at 10:56
0

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()
James Lingham
  • 419
  • 1
  • 3
  • 17
  • well this didn't work for me sorry – dinonuggie Apr 29 '22 at 10:21
  • @dinonuggie I've updated the examples to work fully on their own - I didn't define "action" as a function last time to give a quick example of what i meant, but i've included everything you need to test now, you can confirm this by running the code on its own somewhere – James Lingham Apr 29 '22 at 10:28