Wednesday, September 23, 2020

Since the introduction of the Gregorian calendar (in 1582), the following rule is used to determine the kind of year: if the year number isn't divisible by four, it's a common year; otherwise, if the year number isn't divisible by 100, it's a leap year; otherwise, if the year number isn't divisible by 400, it's a common year; otherwise, it's a leap year. Look at the code in the editor - it only reads a year number, and needs to be completed with the instructions implementing the test we've just described. The code should output one of two possible messages, which are Leap year or Common year, depending on the value entered. It would be good to verify if the entered year falls into the Gregorian era, and output a warning otherwise: Not within the Gregorian calendar period. Tip: use the != and % operators.

LAB  3.1.1.13

Estimated time

10-15 minutes


Level of difficulty

Easy/Medium


Objectives

Familiarize the student with:

using the if-elif-else statement;

finding the proper implementation of verbally defined rules;

testing code using sample input and output.

Scenario:

As you surely know, due to some astronomical reasons, years may be leap or common. The former are 366 days long, while the latter are 365 days long.

 Since the introduction of the Gregorian calendar (in 1582), the following rule is used to determine the kind of year:

  • if the year number isn't divisible by four, it's a common year;
  • otherwise, if the year number isn't divisible by 100, it's a leap year;
  • otherwise, if the year number isn't divisible by 400, it's a common year;
  • otherwise, it's a leap year.

Look at the code in the editor - it only reads a year number, and needs to be completed with the instructions implementing the test we've just described.

The code should output one of two possible messages, which are Leap year or Common year, depending on the value entered.

It would be good to verify if the entered year falls into the Gregorian era, and output a warning otherwise: Not within the Gregorian calendar period. Tip: use the != and % operators.

Test your code using the data we've provided.

year = int(input("Enter a year: "))
if year % 4 != 0:
print("It's a common year")
elif year % 100 != 0:
print("It's a leap year")
elif year % 400 != 0:
print("It's a leap year")
else:
print("It's a leap year")
if year < 1582:
print("But",year, "is not within the Gregorian calendar period")
Enter a year: 2000
It's a leap year

Enter a year: 2015
It's a common year

Enter a year: 1999
It's a common year

Enter a year: 1996
It's a leap year

Enter a year: 1582
It's a common year

Enter a year: 1580
It's a leap year
But 1580 is not within the Gregorian calendar period


    

 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home