3

I'm having a hard time figuring out how to pass a function's return as a parameter to another function. I've searched a lot of threads that are deviations of this problem but I can't think of a solution from them. My code isn't good yet, but I just need help on the line where the error is occurring to start with.

Instructions:

  • create a function that asks the user to enter their birthday and returns a date object. Validate user input as well. This function must NOT take any parameters.
  • create another function that takes the date object as a parameter. Calculate the age of the user using their birth year and the current year.
def func1():
    bd = input("When is your birthday? ")
    try:
        dt.datetime.strptime(bd, "%m/%d/%Y")
    except ValueError as e:
        print("There is a ValueError. Please format as MM/DD/YYY")
    except Exception as e:
        print(e)
    return bd

def func2(bd):
    today = dt.datetime.today()
    age = today.year - bd.year
    return age

This is the Error I get:

TypeError: func2() missing 1 required positional argument: 'bday'

So far, I've tried:

  • assigning the func1 to a variable and passing the variable as func2 parameter
  • calling func1 inside func2
  • defining func1 inside func2
4
  • def func2(bd): alone cannot cause that error. That error is caused when the function is called. Show where func2 is called. That error means though that you aren't passing the required argument to the function. Also note that dt.datetime.strptime(bday, "%m/%d/%Y") isn't doing anything in func1 since you never use the results from it. Commented Dec 3, 2022 at 1:18
  • In the func2 function, you are trying to access the bd parameter, but the error message says that the parameter is called bday. This means that you need to change the bd parameter in the func2 function to bday: Try changing the function call todef func2(bday) : #ERROR OCCURS HERE Commented Dec 3, 2022 at 1:19
  • The error refers to a missing argument bday but the posted code defines the argument name as bd. The code does not match the error. It is much harder to help when you do not post your actual code. Commented Dec 3, 2022 at 1:19
  • HI folks, sorry for the edit. Variable names got lost when I was trying different solutions. My actual solution is exactly Prado910's answer, but I'm still getting the same TypeError age() missing 1 required positional argument: 'bd'. Commented Dec 3, 2022 at 19:24

2 Answers 2

1

You're almost there, a few subtleties to consider:

  1. The datetime object must be assigned to a variable and returned.
  2. Your code was not assigning the datetime object, but returning a str object for input into func2. Which would have thrown an attribute error as a str has no year attribute.
  3. Simply subtracting the years will not always give the age. What if the individual's date of birth has not yet come? In this case, 1 must be subtracted. (Notice the code update below).

For example:

from datetime import datetime as dt

def func1():
    bday = input("When is your birthday? Enter as MM/DD/YYYY: ")
    try:
        # Assign the datetime object.
        dte = dt.strptime(bday, "%m/%d/%Y")
    except ValueError as e:
        print("There is a ValueError. Please format as MM/DD/YYYY")
    except Exception as e:
        print(e)
    return dte  # <-- Return the datetime, not a string.

def func2(bdate):
    today = dt.today()
    # Account for the date of birth not yet arriving.
    age = today.year - bdate.year - ((today.month, today.day) < (bdate.month, bdate.day))
    return age

Can be called using:

func2(bdate=func1())
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! The variable assignment for the datetime object was the part I was stuck in. Not sure why I missed that when I did it for datetime.today(). Thanks for your help
0

I think what you want to do is use it as an argument. You can do it like this:

import datetime as dt

def func1():
    bd = input("When is your birthday? ")
    try:
        date_object = dt.datetime.strptime(bd, "%m/%d/%Y")
    except ValueError as e:
        print("There is a ValueError. Please format as MM/DD/YYY")
    except Exception as e:
        print(e)
    return date_object

def func2(bd)
    today = dt.datetime.today()
    age = today.year - bd.year
    return age

func2(func1())

4 Comments

This will not work. 1) func1 is not called. If it were, 2) it’ll raise an attribute error as str does not have a year attribute. And 3) there is a chance of the wrong age being calculated as only the year is used (once error 2 is resolved).
Thanks for helping out! Apologies as I was trying out different solutions and forgot to fix my variable names, but my actual solution was exactly this and so I'm still unsure where the error is coming from. When I run the code, func1() works fine but I call "func2()" (just exactly this) on the shell and get the TypeError. Is there something I'm missing when I call func2() in the shell? And sorry for the confusion, the instructions says that func1() should return a date object, and that date object should be a parameter of func2().
Can you explain where func1() should be called? I agree, thanks @S3DEV. I have to fix my age expression after resolving the TypeError. Pradio910's solution is exactly what my solution is. Also, I was thinking of taking the birthday through 3 different input statements instead of just one, then use that for the age expression since str doesn't have a year attribute like you mentioned. Thanks a lot for your help
@giovanni - A problem is that the datetime object is never assigned. It happens, but it’s not stored. The return of datetime.strptime call must be stored into a variable, then returned. Then that will be the input into func2. (Working on an answer for you now ...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.