Computer Science, asked by gazdon617, 3 months ago

Assignment 7: Calendar edhesive python

Attachments:

Answers

Answered by KingFriendly
101

Answer:

def leap_year(y):

   if y % 4 == 0:

       return 1

   else:

       return 0

def number_of_days(m,y):

   if m == 2:

       return 28 + leap_year(y)

   elif m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m ==10 or m == 12:

       return 31

   elif m == 4 or m == 6 or m == 9 or m == 11:

       return 30

def days(m,d):

   if m == 1:

       return 0 + d

   if m == 2:

       return 31 + d

   if m == 3:

       return 59 + d

   if m == 4:

       return 90 + d

   if m == 5:

       return 120 + d

   if m == 6:

       return 151 + d

   if m == 7:

       return 181 + d

   if m == 8:

       return 212 + d

   if m == 9:

       return 243 + d

   if m == 10:

       return 273 + d

   if m == 11:

       return 304 + d

   if m == 12:

       return 334 + d

def days_left(d,m,y):

   if days(m,d) <= 60:

       return 365 - days(m,d) + leap_year(y)

   else:

       return 365 - days(m,d)

print("Please enter a date")

day=int(input("Day: "))

month=int(input("Month: "))

year=int(input("Year: "))

choice=int(input("Menu:\n1) Calculate the number of days in the given month.\n2) Calculate the number of days left in the given year.\n"))

if choice == 1:

   print(number_of_days(month, year))

if choice == 2:

   print(days_left(day,month,year))

Explanation:

1. A leap year happens every four years, so you'll want a function that returns 0 or 1 based on whether a year is divisible by four.

2. To find the number of days in a month, you make a function that contains if statements. If your variable "month" is 1, 3, 5, 7, 8, 10, or 12, it will return 31, as there are 31 days in each of those months. In your if statement for month 2, February, you will add leap_year(y) to 28. If it is not a leap year, it will add 0 to the number of days in February, and if it is, it will add one day, making it 29 days.

3. To find the number of days left in a year, you take the days that have already passed and subtract them from 365. If it is a leap year, however, there will be an extra day added. So, then, you add 1 to the difference of 365 and days(m,d) ONLY if the number of days passed is less than or equal to 60. This is because there's only 1 day added to days left in the month before that day has already passed. Once the leap day is passed, the number of days left works like normal.

I hope I explained well enough. It took me many, many tries to get this correct!

Answered by allysia
8

Language:

Python

Program:

#function1

def leap_year(y):

   if y%4==0:

       return 1

   else:

       return 0

#function2

def number_of_day(m,y):

   if m in [1,3,5,7,8,10,12]:  #1=jan, 3=march and so on.

       return 31

   elif m==2:

       if leap_year(y) == 1:  #ranleap_year(y)to check th no of days in feb.

           return 29

       else:

           return 28

   else:

       return 30

   

#fuction3.

def day_left(d,m,y):

   sum= 0

   left_in_month= number_of_day(m,y)-d #(number of days left in that month )

   for i in range(m, 12):

       sum += number_of_day(i+1,y) #(i+1 to ignore that month and add days of remaining months of the year)

   return sum + left_in_month

#preparing for user input

print("Please enter a date")

d=int(input("Date:"))

m=int(input("Month:"))

y=int(input("Year:"))

print("Menu: \n1)Calculate the number of days in the given month. \n2)Calculate the number of days left in the given month.")

choice=int(input())

if choice==1:

   print(number_of_day(m,y))

elif choice==2:

   print(day_left(d,m,y))

else:

   print('Not valid choice')

Output:

Please enter a date

Date:5

Month:5

Year:1984

Menu:  

1)Calculate the number of days in the given month.  

2)Calculate the number of days left in the given month.

1

31

Explanation:

  • defined leap_year() function and used it in the second function to get the days in month.
  • Using the second function with a loop in the third function to get the number of days left in  a year.
  • Under function number_of_day(m,y) the list shows the month numbers corresponding to get the number of days in that paticular month.

Attachments:

Attachments:
Similar questions