如果在创建闰年程序时python程序中的函数不能正常工作 在这里,我已经进入2019年,这不是闰年,但我的计划没有给我这个,如果你进入一个非闰年,它想把它作为非闰年 如果我的程序中有任何错误,请告诉我 这是我创建程序时使用的以下要点中的注意事项--> 年份必须能被4整除 如果一年也可以平均除以100,那就不是闰年; 除非 这一年也可以被400整除。那就是闰年了
您忘了在如果在创建闰年程序时python程序中的函数不能正常工作 在这里,我已经进入2019年,这不是闰年,但我的计划没有给我这个,如果你进入一个非闰年,它想把它作为非闰年 如果我的程序中有任何错误,请告诉我 这是我创建程序时使用的以下要点中的注意事项--> 年份必须能被4整除 如果一年也可以平均除以100,那就不是闰年; 除非 这一年也可以被400整除。那就是闰年了,python,function,if-statement,leap-year,Python,Function,If Statement,Leap Year,您忘了在else部分中写入print。应该是: def is_leap(year): if year % 4 == 0: print("year is a leap year") elif year % 400 == 0: print("year is a leap year") elif year % 100 == 0: print("not a leap year&quo
else
部分中写入print
。应该是:
def is_leap(year):
if year % 4 == 0:
print("year is a leap year")
elif year % 400 == 0:
print("year is a leap year")
elif year % 100 == 0:
print("not a leap year")
else:
print("Not a leap year")
is_leap(2019)
首先,您的代码中有错误,为了检查一年是否是闰年,您应该遵循以下步骤:
if-else
语句。查看以下简单代码,并尝试推断出错误的具体位置:
print("Not a leap year")
正如@Alexander Kononenko所说,您的else
中缺少了一个打印
也许可以这样试试
您可能只是忘记了在
else
中打印:打印(“不是闰年”)
您的else分支只是生成一个字符串并将其扔掉,在调用函数的第一个开始括号之前添加打印
。没有机会让“2019”进入elif
:2019%4=3;2019 % 400 = 19; 2019%100=19教我一些我对stack不了解的东西overflow@NimeshJoshi您将接受从我的答案中删除,以简化if案例。背后的理性是什么?如果代码不正确,请尝试输入1900。
def is_leap(year):
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
return
is_leap(2019)
year = int(input("Input year: "))
if year % 4 == 0:
print("Year is leap.")
if year % 100 == 0 and year % 400 != 0:
print("Year is common.")
else:
print("Year is common.")