How do I return multiple variables from a single function in python 3? -
this question has answer here:
- how return multiple values in python? 12 answers
i student learning basics of python. understand how functions work, not know how return multiple variables single function. able reach same effect using copy + paste, that's not accepted teacher. wish run function twice, , want program save both of outputs, in seconds. below program in question, , when enter birthday, give age in seconds, minutes, hours, days, weeks, months, , years.
#age finder program #this program calculate 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 name?') print('nice meet you, ', name,) print('enter following information calculate approximate age!') month_birth = int(input('enter numerical month in born: ')) day_birth = int(input('enter numerical day in relation month of born: ')) year_birth = int(input('enter year in born : ')) #determine number of seconds in day, average month, , 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,', 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()
in python can return variables in tuples so:
def func(): return a, b, c, d
and unpack them so:
e, f, g, h = func()
Comments
Post a Comment