4

I'm trying to make a simple function where python would calculate age depending on your year input. I've tried several ways and I haven't had luck atm.

ps. sorry, I'm a newbie at this.

ame = input(" Enter your name: ")

age = input(" When were you born?: ")

print("Hello " + name + "! You are " + input (2021 - age)
2
  • 2
    print("Hello " + name + "! You are ",2021- int(age)) Commented Jun 29, 2021 at 17:22
  • 1
    Another overly literal use of [python-requests] tag, please read the tag description Commented Jun 29, 2021 at 17:24

6 Answers 6

2
import datetime

# get the year so it works all the time.
year = datetime.datetime.today().year
name = input(" Enter your name: ")
birth_year = input(" When were you born (year) ?: ")

# calclute the age
age = year - int(birth_year)

print("Hello " + name + "! You are ", age)

There are other ways to print which might look more clean:

print(f'Hello {name}! You are {age}')

or

print("Hello {0}! You are {1}".format(name, age))

To make it a function:

import datetime

def age_cal(birth_year):
    # get the year so it works all the time.
    year = datetime.datetime.today().year
    return year - int(birth_year)

if __name__ == "__main__":
    name = input(" Enter your name: ")
    birth_year = input(" When were you born (year) ?: ")
    age = age_cal(birth_year)

    print("Hello {0}! You are {1}".format(name, age))
Sign up to request clarification or add additional context in comments.

Comments

1

Here is what you can do: convert age to integer, then subtract it from 2021.

ame = input(" Enter your name: ")
age = input(" When were you born (year) ?: ")
print("Hello " + name + "! You are ",2021- int(age))

2 Comments

f-strings are more readable. Consider using print(f"Hello {name}! You are {2021-int(age)}")
@not_speshal, thank you for your suggestion. I am aware of that. Actually, f-strings are a little advanced. So I deterred from using it
1

Let’s go through the code sample you provided line-by-line.

name = input(" Enter your name: ")

This line creates a variable called name and assigns it to the return value of the built-in function input.

age = input(" When were you born? ")

This does the same, but for the variable age. Note that this stores a string (some characters) not an int. You’ll probably want to convert it, like this:

age = int(age)

Next, you’re trying to print:

print("Hello " + name + "! You are " + input (2021 - age)

But to figure out what to print, Python has to evaluate the return of input(2021-age). (Remember in your code that age was a string; you can’t subtract strings and ints).

You’ve got another problem here- you’re prompting and waiting for input again, but you don’t need to. You’ve already stored the user’s input in the age and name variables.

So what you really want to do is:

print("Hello, " + name + "! You are " + 2021 - age )

Now, if you wanted to be a little more concise, you could also do:

print(f"Hello, {name}! You are {2021 - age}")

Comments

0

You can convert input to int and format your print statement, like below.

name = input("Enter your name: ")
age = int(input("When were you born? (year): "))  # convert input to int
print("Hello {}! You are {} years old".format(name, (2021-age)))  # format output

Comments

0

You can make a simple function with datatime like this:

from datetime import date

name = input(" Enter your name: ")
year = int(input(" When were you born?: "))

def calculateAge(year):
    today = date.today()
    age = today.year - year
    return age

print("Hello " + name + "! You are " + str(calculateAge(year)))

Comments

-3

This isn't a function for that you have to use def anyway this is the code Code-

from datetime import date
todays_date = date.today()
name = input(" Enter your name: ")
dob = int(input(" When were you born?: "))
print("Hello " + name + "! You are " + todays_date.year - dob)

1 Comment

Please test your answers before posting. What is num(2021)?

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.