【问题标题】:For loop - Leap year inputFor 循环 - 闰年输入
【发布时间】:2020-01-11 22:50:41
【问题描述】:

谁能帮我解决我目前遇到的一个问题……这是一个闰年练习。

● 编写一个程序来输入年份和年份数。

● 然后确定并显示哪些年份是闰年或将是闰年 年。

示例: 你想从哪一年开始? - 1994

您要检查多少年? - 8

1994 年不是闰年
1995 年不是闰年
1996年是闰年
1997 年不是闰年
1998 年不是闰年
1999 年不是闰年
2000年是闰年
2001 年不是闰年

我似乎无法让我的年份显示自己是否是闰年的价值......这是我到目前为止的代码:

year = int(input("Please enter the year you would like to start checking leap years from."))
total_years = int(input("Please enter over how many years you would like to check."))
leap_year= 0

if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):   
   leap_year = ("this is a leap year")

else:
   leap_year = ("this is not a leap year")

for a in range (0,total_years):
    print(year + a, leap_year)

任何帮助将不胜感激。 谢谢你。

【问题讨论】:

  • 您能否提供更多信息?您是否收到任何错误消息?如果代码运行,您可以发布输出吗? Python 是一种对制表符敏感的语言(即制表符很重要)。您的代码格式似乎不正确,但这可能是复制/粘贴问题
  • 这是一个复制粘贴问题...对不起。代码运行,但它正确显示年份,但只取您输入的初始年份的值并将该值添加到所有年份......
  • 如果输入 1992...(1992 是闰年)超过 3 年显示:
  • 1992 年是闰年。 1993年是闰年。 1994 年是闰年......如果输入 1991(1991 年不是闰年)。它显示:1991 年不是闰年。 1992年不是闰年。 1993 年不是闰年

标签: python for-loop


【解决方案1】:

检查闰年的 if-else 块应该在 for 循环中。它目前在 for 循环之外,因此它会检查您输入的第一年,for 循环只会打印第一年的结果 total_years 次数。

因此,要修复您的代码,请尝试以下操作:

year = int(input("Please enter the year you would like to start checking leap years from."))
total_years = int(input("Please enter over how many years you would like to check."))

def is_leap_year(year):
    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
       return True
    else:
       return False

for a in range (0, total_years):
    if is_leap_year(year+a):
        print("this is a leap year")
    else:
        print("this is not a leap year")

【讨论】:

  • 好的...谢谢你能告诉我你会如何编码吗?非常感谢!
  • 是的,我刚刚发布了一种方法。我将闰年检查代码放入一个函数中,并从 for 循环中调用该函数
  • 非常感谢它的完美。现在就运行它,它运行良好......我唯一改变的是你的打印语句来显示年份和多少年:
  • print (a + year, "这是闰年") else: print (a + year, "这不是闰年")
【解决方案2】:

您需要将检查年份的逻辑放入循环中。比如像这样:

start_year = int(input("Please select a starting year: "))
num_of_years = int(input("Please select how many years you'd like to check: "))
end_year = start_year + num_of_years

# The formula I used below to determine when a year is a leap year was copied from emrah-diril in this thread

for year in range(start_year-1, end_year):
    year += 1    
    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
        print ("{}: Is a leap year".format(year))
    else:
        print (year)

【讨论】:

    【解决方案3】:

    蟒蛇

    start_year = int(input("Please enter the year you would like to start checking leap years from: "))
    total_years = int(input("Please enter over how many years you would like to check: ")) 
    
    for year in range(start_year, start_year + total_years):
    
            if year % 4 == 0:       
                    print(f"{year} is a leap year") 
            else:
                    print (f"{year} is not a leap year")
    

    【讨论】:

    • 这是对您问题的简化答案。
    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多