Home > Net >  Is there a function to get specific number values associated with the names in my list?
Is there a function to get specific number values associated with the names in my list?

Time:04-30

birthdays = [
    ('Bobby White', '08/06/2003'),
    ('George Washington', '02/22/1732'),
    ('Kim Kardashian', '10/21/1980')

def age():
    global birthdays
    name = birthdays[0]
    dateList = (name[1].split("/"))         
    aDay = int(dateList[0])
    aMonth = int(dateList[1])
    aYear = int(dateList[2])
    print(birthdays)
    Person = input("Who's age do you want to find?(Input their exact name)")
    for item in birthdays:
        if item == Person:
            aYear
            AgeCalc = 2022 - aYear
            print(Person, "is/will be", AgeCalc, "this year.")

I want to pull the year located in my list for any item in it so i can subtract the year by the current year to find their age. I'm somewhat new to using Python and couldn't find a function to do this. Any help is appreciated!

CodePudding user response:

You need to get the year of the person that matches Person, not the first person in the birthdays list.

def age():
    Person = input("Who's age do you want to find?(Input their exact name)")
    for name, birthday in birthdays:
        if name == Person:
            aDay, aMonth, aYear = map(int, birthday.split('/'))
            AgeCalc = 2022 - aYear
            print(Person, "is/will be", AgeCalc, "this year.")

CodePudding user response:

Perhaps use a Dictionary instead of going over a list? It's much more "pythonic" and efficient.

something like:

birthdays = 
    { 'Bobby White': '08/06/2003',
    'George Washington': '02/22/1732',
    'Kim Kardashian': '10/21/1980' }

Person = input("Who's age do you want to find?(Input their exact name)")

print (birthdays[Person])

  • Related