Someone Help with this Code

In the code below if I enter 0 it runs through the loop and the value is the value the user inputs. however after entering 0 if I enter any other year the ageyear = getUseryear() line get the value of None or 0. What am I doing wrong… ageyear should be equal to the value the user enters.

from datetime import date
current_date = date.today()
print("the current date is: ", current_date)def getUseryear():
    brthyr = input("enter the birth year")
    print ("Birthyr", brthyr)
    useryr = int(brthyr)
    print("useryr after integer casting", useryr)
    if useryr <= 0:
     getUseryear()
     elif useryr > current_date.year:
        getUseryear()
    else:
     return useryr


ageyear = getUseryear()

print(f"The value is : {ageyear}")

You do not update the useryr variable with the result of getUseryear(), which I assume gets a valid year.

Ugghh such a simple solve ughhh why did iInot see it…

Nope I tried with useryr = getUseryear after the if and the elif… and the variable that originally calls the function is still type None …

Does getUseryear actually return the year?

Only if you enter a valid year the first time.

We need to see all the code to be able to give better feedback.

It seems that your logic for inputing a valid year is not doing what you need. You might be able to improve the code by reviewing your logic in that area of code.

I have added the imports to the code. This is the full code.
I need “ageyear” to have the correct year when entered after entering incorrect values.
When I enter incorrect values that triggers the if or elif conditions then i enter a correct value it returns none to the ageyear value.

e.g. enter 0 then when prompted enter 2026 both incorrect values, then enter a correct value and see the “error” and the value of “Ageyear”

Your copy and paste messed up the formatting. I did not see the “def” first time I read your post.

(You can edit you post to fix that).

I see that you are calling the getUseryear() function recursively.
In which case you need to return the value of getUseryear() each time you call it. Otherwise the new value will be lost.

Another issue in the script is that the conditional statements are not unique:

    if user_year <= 0:
        get_user_year()
    elif user_year > current_date.year:
        get_user_year()

Notice how you call the function recursively for two different conditions. If you want to call it for those two conditions, then it would be better if you combined them via or as in:

if user_year <= 0 or user_year > current_date.year:
    get_user_year()

You can also typecast the input at the very beginning so that you don’t have to do it separately in another line of code:

birth_year = int(input("Enter the birth year: "))  # typecast user input to type 'int'

It also helps if your indentations are consistent throughout your script.

I have tried all of that and when the correct year is entered the return value is still NONE after entering two incorrect values then the correct value. I’m missing something fundamentally here about functions and returning integer values. I have tried type casting combining the conditional on one line then the return still the same result when and only when I enter incorrect values first.

from datetime import date
current_date = date.today()
print("the current date is: ", current_date)


def getuseryear():
    useryr = int(input("enter the birth year"))
    if useryr <= 0 or useryr > current_date.year:
        getuseryear()
    else:
     return useryr 

ageyear = getuseryear()

print(f"The value is : {ageyear}")

Even this has the same result where ageyear value is None after the incorrect values

Here is a thread that discusses this issue:

I have modified your script such that it no longer returns the None.

from datetime import date
current_date = date.today()
print("The current date is: ", current_date)

def get_user_year():

    useryr = int(input("Enter the birth year: "))

    if useryr <= 0 or useryr > current_date.year:
        return get_user_year()
    else:
        return useryr

ageyear = get_user_year()

print(f"The value is : {ageyear}")

Notice that instead of just calling the function recursively, we return it.

You are not returning the value from getuseryear().

    if useryr <= 0 or useryr > current_date.year:
        return getuseryear()
        #^^^^^^^

Oh I see what you mean now …

Man I cannot thank you enough!!! That was very frustrating, and I learned something new from it. So, Thank you very much!!

I’m still learning python …