我写了这个程序来计算你的生活天数,当你使用当前日期和你的出生日期作为输入时(不管是哪一个,只要它的格式是:y,m,d。这是我的第一个代码,所以它的结构可能很差,但是我试图看看为什么它没有返回?
def past_presenter(year1,month1,day1,year2,month2,day2):
if year1>year2:
year1,month1,day1,year2,month2,day2 = year2,month2,day2,year1,month1,day1
return year1,month1,day1,year2,month2,day2
else:
if month1>month2:
year1,month1,day1,year2,month2,day2 = year2,month2,day2,year1,month1,day1
return year1,month1,day1,year2,month2,day2
else:
if day1>day2:
year1,month1,day1,year2,month2,day2 = year2,month2,day2,year1,month1,day1
return year1,month1,day1,year2,month2,day2
def absolute(y,m,d):
if y<0:
y=y*-1
if m<0:
m=m*-1
if d<0:
d=d*-1
return y,m,d
def days(year1,month1,day1,year2,month2,day2):
y=year2-year1
m=month2-month1
d=day2-day1
y,m,d = absolute(y,m,d)
days=d+(m*30)+(y*30*12)
return days
def daysBetweenDates(year1,month1,day1,year2,month2,day2):
year1,month1,day1,year2,month2,day2 = past_presenter(year1,month1,day1,year2,month2,day2)
dd = days(year1,month1,day1,year2,month2,day2)
return dd
print daysBetweenDates(2017,2,10,1980,1,1)
答案 0 :(得分:1)
当我复制粘贴代码并运行它时,我的测试用例会返回13359。但是,我确实看到了你的错误。测试用例,其中year1&lt; year2将返回None,因为past_presenter返回None,在所有if语句的末尾都缺少return语句。所有返回语句都嵌套在“if year1&gt; year2”中,因此当year2&gt; = year1时它不会返回任何内容。
答案 1 :(得分:0)
问题已解决,因为代码很好,但正在测试的平台:“https://repl.it/languages/python”无论代码是什么都会输出