( Converting a given date to the day number in the year) Write a python program
ID: 3682407 • Letter: #
Question
( Converting a given date to the day number in the year)
Write a python program that prints the day number of the year, given the date is in the form month day year. For example, if the input is 1 1 05, the day number is 1; if the input is 12 25 05, the day number is 359. The program should check for a leap year. A year is a leap year if it is divisible by 4 but not divisible by 100. For example, 1992 and 2008 are divisible by 4, but not by 100. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example, 1600 and 2000 are divisible by 400. However, 1800 is not a leap year because 1800 is not divisible by 400.
Directions:
Call 3 functions from your main.
One to input data (pass parameters in a list)
One to determine the result (use a value returning function to determine and return the result)
One to display both the date and the result
Explanation / Answer
def is_leap_year(year):
""" if year is a leap year return True
else return False """
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
def doy(d):
ins = d.split(" ")
Y = int(ins[2])
M = int(ins[0])
D = int(ins[1])
if is_leap_year(Y):
K = 1
else:
K = 2
N = int((275 * M) / 9.0) - K * int((M + 9) / 12.0) + D - 30
return N
def getDate():
d=raw_input('Please enter a date:')
return d
def display(n):
print "Day of the year is %d" %(n)
return
d = getDate()
n = doy(d)
display(n)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.