Python input function, printing

Question:

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)
Asked By: gintoki

||

Answers:

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))
Answered By: user15801675

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
Answered By: Chandella07

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)
Answered By: Md Owais
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))
Answered By: Navid Zarepak

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}")
Answered By: Ezra

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)))
Answered By: FoodHunter
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.