在Python3中,如何从单个函数返回多个变量?
我是一名学生,目前正在学习Python的基础知识。我了解函数是如何工作的,但我不知道如何从单个函数返回多个变量。我可以通过使用复制+粘贴达到同样的效果,但老师不接受。我希望运行该函数两次,并希望程序保存两个输出,但只需几秒钟。下面是正在讨论的程序,当你进入你的生日时,它将以秒、分钟、小时、天、周、月和年为单位显示你的年龄在Python3中,如何从单个函数返回多个变量?,python,function,Python,Function,我是一名学生,目前正在学习Python的基础知识。我了解函数是如何工作的,但我不知道如何从单个函数返回多个变量。我可以通过使用复制+粘贴达到同样的效果,但老师不接受。我希望运行该函数两次,并希望程序保存两个输出,但只需几秒钟。下面是正在讨论的程序,当你进入你的生日时,它将以秒、分钟、小时、天、周、月和年为单位显示你的年龄 #Age Finder Program #This program will calculate a person's age import datetime #Get
#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()
年龄查找程序
#这个程序将计算一个人的年龄
导入日期时间
#获取当前日期
当前月份=datetime.date.today().month
当前日=datetime.date.today().day
当前年份=datetime.date.today().year
定义年龄():
#获取信息
name=input('你叫什么名字?')
打印('很高兴认识你',姓名,)
打印('输入以下信息以计算您的大致年龄!')
month_birth=int(输入('输入您出生的月份:'))
day_birth=int(输入('输入与您出生月份相关的数字日期:'))
year_birth=int(输入('输入您出生的年份:'))
#确定一天、一个月和一年中的秒数
日数=24*60*60
numsec_年=365*numsec_天
平均单位年=((4*单位年)+单位日)//4
平均月=平均年//12
#以秒为单位计算大致年龄
努姆塞克斯1900努姆塞克斯出生日期=((出生年份-1900)*平均努姆塞克斯年)+\
((出生月份-1)*平均数月)+\
(出生日*numsec\u日)
今天的货币单位1900=((当前年份-1900)*平均货币单位年份)+\
((本月-1)*平均月数+\
(当前日*单位日)
年龄(以秒计)=numsecs(以秒计)1900(以秒计)-numsecs(以秒计)1900(以秒计)dob(以秒计)
用分钟表示的年龄=用秒表示的年龄/60
以小时为单位的寿命=以分钟为单位的寿命/60
以天为单位的年龄=以小时为单位的年龄/24
周龄=日龄/7
月龄=周龄/4.35
年龄=月龄/12
#输出
打印(‘嗯,’,名字,’,你大约,’岁,’秒,’秒!)
打印('Or',以分钟为单位显示年龄,'minutes old!')
打印('或',以小时为单位的老化,'小时老化!')
打印('或',以天为单位的年龄,'天为单位!')
打印('Or',年龄单位为周,'周龄!')
打印('Or',年龄单位为月,'月龄!')
打印('Or',年龄,'years!')
#额外的
如果年龄小于18岁:
打印('在学校玩得开心!\n')
年龄()
在python中,您可以返回元组中的变量,如下所示:
def func():
return a, b, c, d
然后像这样打开包装:
e, f, g, h = func()
python函数返回一个元组作为多个值的组合。您的函数似乎不返回任何值