Python: 如何在另一个函数中包含一个函数?
Python: How do I include a function in another function?
我刚开始学习编程,我有一个关于函数的问题。我定义函数闰年如下:
def leapyear(n):
# Given a year, decide if it is a leap year or not.
if n%4==0:
if n%100==0:
if n%400==0:
return "It is a leap year."
else:
return "It is not a leap year"
else:
return "It is a leap year"
else:
return "It is not a leap year"
现在我想在另一个函数中使用它,它会告诉我我写的日期是否有效(例如,02/28/2021 有效,但 02/29/2021 无效) .我所做的是:
def valid_date():
day = int(input("Day: "))
month = input("Month: ")
year = int(input("Year: "))
if month=="January":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
if month=="February":
if year%4==0:
if year%100==0:
if year%400==0:
if day<=29:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=28:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=29:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=28:
print("Fecha válida")
else:
print("Fecha no válida")
if month=="March":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
其实到12月了,不过没关系,基本一样
代码有效,但我想使用闰年函数而不是重新编写整个代码。我该怎么做?
谢谢!
首先,您的函数应该 return True
或 False
;让调用者决定是否需要像 "It's a leap year"
这样的字符串表示形式。
def leapyear(n):
# Given a year, decide if it is a leap year or not.
if n%4==0:
if n%100==0:
if n%400==0:
return True
else:
return False
else:
return True
else:
return False
在您的 valid_date
函数中,您可以调用 leapyear
并使用其结果来确定该年的二月是否可以有 29 日。
def valid_date():
day = int(input("Day: "))
month = input("Month: ")
year = int(input("Year: "))
if month=="January":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
if month=="February":
<b>if leapyear(year):
last_day = 29
else:
last_day = 28</b>
if day <= <b>last_day</b>:
print("Valid date")
else:
print("Not a valid date")
if month=="March":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
让你的手指懒惰。较短的程序通常更易于阅读。
def leapyear( y):
return (y%4==0 and y%100!=0) or y%400==0
def valid_date( year, month, day):
if (day <= 0) or (month <= 0) or (year < 1582): return False
last = [31,29 if leapyear( year) else 28,31,30,31,30,31,31,30,31,30,31]
return (month <= 12) and (day <= last[month-1])
year, month, day = input( "Enter date (yyyy-mm-dd):").split( '-')
if valid_date( int( year), int( month), int( day)):
print( "Fecha válida")
else: print( "Fecha no válida")
leapyear() 代码仅对从 1582 年(pope Grégoire)开始的年份有效。您可以添加一些逻辑来验证更早的日期。
您的程序还应该包括对用户输入的一些验证。例如,字母字符会使程序崩溃。
我刚开始学习编程,我有一个关于函数的问题。我定义函数闰年如下:
def leapyear(n):
# Given a year, decide if it is a leap year or not.
if n%4==0:
if n%100==0:
if n%400==0:
return "It is a leap year."
else:
return "It is not a leap year"
else:
return "It is a leap year"
else:
return "It is not a leap year"
现在我想在另一个函数中使用它,它会告诉我我写的日期是否有效(例如,02/28/2021 有效,但 02/29/2021 无效) .我所做的是:
def valid_date():
day = int(input("Day: "))
month = input("Month: ")
year = int(input("Year: "))
if month=="January":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
if month=="February":
if year%4==0:
if year%100==0:
if year%400==0:
if day<=29:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=28:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=29:
print("Valid date")
else:
print("Not a valid date")
else:
if day<=28:
print("Fecha válida")
else:
print("Fecha no válida")
if month=="March":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
其实到12月了,不过没关系,基本一样
代码有效,但我想使用闰年函数而不是重新编写整个代码。我该怎么做?
谢谢!
首先,您的函数应该 return True
或 False
;让调用者决定是否需要像 "It's a leap year"
这样的字符串表示形式。
def leapyear(n):
# Given a year, decide if it is a leap year or not.
if n%4==0:
if n%100==0:
if n%400==0:
return True
else:
return False
else:
return True
else:
return False
在您的 valid_date
函数中,您可以调用 leapyear
并使用其结果来确定该年的二月是否可以有 29 日。
def valid_date():
day = int(input("Day: "))
month = input("Month: ")
year = int(input("Year: "))
if month=="January":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
if month=="February":
<b>if leapyear(year):
last_day = 29
else:
last_day = 28</b>
if day <= <b>last_day</b>:
print("Valid date")
else:
print("Not a valid date")
if month=="March":
if day<=31:
print("Valid date")
else:
print("Not a valid date")
让你的手指懒惰。较短的程序通常更易于阅读。
def leapyear( y):
return (y%4==0 and y%100!=0) or y%400==0
def valid_date( year, month, day):
if (day <= 0) or (month <= 0) or (year < 1582): return False
last = [31,29 if leapyear( year) else 28,31,30,31,30,31,31,30,31,30,31]
return (month <= 12) and (day <= last[month-1])
year, month, day = input( "Enter date (yyyy-mm-dd):").split( '-')
if valid_date( int( year), int( month), int( day)):
print( "Fecha válida")
else: print( "Fecha no válida")
leapyear() 代码仅对从 1582 年(pope Grégoire)开始的年份有效。您可以添加一些逻辑来验证更早的日期。
您的程序还应该包括对用户输入的一些验证。例如,字母字符会使程序崩溃。