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()
You have some errors in your code, lets go step by step:
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.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
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:
action
passing user_response
as argumentuser_response
inside action
and retrieve the valueuser_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()
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()
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.