Nicki
Nicki

Reputation: 3

Years display in Python from a age and print every year since birth

I want to create a code in Python that asks the user for their age and displays on the screen every year since birth, separated by commas. Using for.

Like this example:

How old are you?: 37 1985, 1986, 1987, 1988, ... 2017, 2018, 2019, 2020, 2021, 2022

Please help me.

Thank you :)

Upvotes: -3

Views: 379

Answers (2)

Tolga Aydemir
Tolga Aydemir

Reputation: 16

You try maybe below code

age = input("How old are you? \n")
year = 2022-int(age)

for x in range(year, 2023):
  print("{},".format(x)) 

Upvotes: -2

nareshg
nareshg

Reputation: 15

from datetime import date 
age = input("what is your age : ")
age = int(age)
out = ""
current_year = date.today().year
birth_year = current_year - age
for i in range(birth_year, current_year+ 1):
    out = out + str(i) + ", "

print(out)

Upvotes: -2

Related Questions