Calculating Leap Year of Year Beginning to Final Year

Asked

Viewed 91 times

-2

Hello, have an exercise where it inform the following:

" Do you know what leap years are? Briefly, in leap years the month of February is longer, going from 28 to 29 days. The calculation of whether a year is leap is also quite simple, as a year is leap if it is divisible by 4 and not by 100, or if it is divisible by 400.

Claudia loves leap years because she was born into one! More precisely, Claudia was born on 29 February, so she feels that her birthday only occurs "for real" in the years that are leap, after all can project the party without having to explain to friends will be celebrated on 28 February or on 1 March.

You were invited to Claudia’s celebration, but for that she asked for a special gift, requested that you build a program that helps her to be less anxious to know what are the years when she can hold parties on the correct date. For this, your program should receive one START year and one END year and display all leap years of the closed interval [ START.. END ]. At the end, the program must also display the number of leap years of the interval.

ENTREE

Two natural numbers, one in each row, representing the BEGINNING year and the END year of the considered interval. Adopt that (0 <= START <= END <= 9999) will always be true.

EXIT

All leap years of closed interval [ START.. END ], one per line. At the end, the program should display the amount of leap years, as the examples. "

EXEMPLOS ANO BISSEXTO

The code that I have achieved so far, is not counting the years and informing at the end the amount that is leap, follows code:

ano_inicio = int(input('Digite o ano: '))
ano_fim = int(input('Digite o ano final: '))
calculo = (ano_inicio % 4 == 0 and ano_inicio % 100 != 0) or (ano_inicio % 400 == 0)
contagem = 0

for x in range(ano_inicio, ano_fim+1):
    if calculo:
        pass
    else:
        contagem +=1
        print(f'{x}')
print(f'bissextos: {contagem}')

1 answer

0


Your code was on the right track, to count the number of leap years what you had to check is whether the year module 4 was equal to zero.

Corrected code:

ano_inicio = int(input("Digite o ano: "))
ano_fim = int(input("Digite o ano final: "))

contagem = 0

for ano in range(ano_inicio, ano_fim + 1):
    if ano % 4 == 0:
        print(ano)
        contagem += 1
      
print(f"Bissextos: {contagem}")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.