1

I am a student who is currently learning the basics of Python. I understand how functions work, but I do not know how to return multiple variables from a single function. I am able to reach the same effect by just using copy + paste, but that's not accepted by the teacher. I wish to run the function twice, and I want the program to save both of the outputs, but only in seconds. Below is the program in question, and when you enter your birthday, it will give you your age in seconds, minutes, hours, days, weeks, months, and years.

#Age Finder Program
#This program will calculate a person's age


import datetime

#Get current dates
current_month = datetime.date.today().month
current_day = datetime.date.today().day
current_year = datetime.date.today().year

def age():

#Get info
name = input('What is your name?')
print('Nice to meet you, ', name,)
print('Enter the following information to calculate your approximate age!')
month_birth = int(input('Enter the numerical month in which you were born:    '))
day_birth = int(input('Enter the numerical day in relation to the month of which you were born: '))
year_birth = int(input('Enter the year in which you were born : '))

#Determine number of seconds in a day, average month, and a year
numsecs_day = 24 * 60 * 60
numsecs_year = 365 * numsecs_day

avg_numsecs_year = ((4 * numsecs_year) + numsecs_day) // 4
avg_numsecs_month = avg_numsecs_year // 12

#Calculate approximate age in seconds
numsecs_1900_dob = ((year_birth - 1900) * avg_numsecs_year) + \
                    ((month_birth - 1) * avg_numsecs_month) + \
                    (day_birth * numsecs_day)

numsecs_1900_today = ((current_year - 1900) * avg_numsecs_year) + \
                    ((current_month - 1) * avg_numsecs_month) + \
                    (current_day * numsecs_day)

age_in_secs = numsecs_1900_today - numsecs_1900_dob
age_in_minutes = age_in_secs / 60
age_in_hours = age_in_minutes / 60
age_in_days = age_in_hours /24
age_in_weeks = age_in_days / 7
age_in_months = age_in_weeks / 4.35
age_in_years = age_in_months / 12

#Output
print('Well,',name,', you are approximately', age_in_secs, 'seconds old!')
print('Or', age_in_minutes, 'minutes old!')
print('Or', age_in_hours, 'hours old!')
print('Or', age_in_days, 'days old!')
print('Or', age_in_weeks, 'weeks old!')
print('Or', age_in_months, 'months old!')
print('Or', age_in_years, ' years old!')

#Extra
if age_in_years < 18:
    print('Have fun in School!\n')

age()
Mark Costello
  • 145
  • 10

1 Answers1

3

In python you can return variables in tuples like so:

def func():
    return a, b, c, d

and unpack them like so:

e, f, g, h = func()
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48