Unit 3 Sections 14-15 Homework
Here are the instructions for the homework for sections 14-15.
Create a program that asks the user for a day and then gives them a distance in days between that day and another random day in the year. We have provided you with a possible starter, but you are welcome to change it up if you would like.
from datetime import date, timedelta, datetime
import random
minDate, maxDate = date(1900, 1, 1), date(2022, 12, 31)
print("Date range: " + str(minDate) + " to " + str(maxDate))
dateDays = maxDate - minDate
totalDays = dateDays.days
randDay = random.randrange(totalDays)
randDate = minDate + timedelta(days=randDay)
print(randDate)
days_dictionary = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}
def inputDate():
day = ""
month = ""
year = ""
datestr = ""
quit = False
# while (datestr != "quit"):
while (True):
print("\nEnter a day, month, and year in the 'dd-mm-yyyy' format or type 'quit' to quit.")
# datestr = input()
d = input()
if (d == "quit"):
print ("Loop exited.")
break
m = input()
if (m == "quit"):
print ("Loop exited.")
break
y = input()
if (y == "quit"):
print ("Loop exited.")
break
diffDate(int(d), int(m), int(y))
def diffDate(day, month, year):
timePass = date(year, month, day)
if (timePass > randDate):
finalTime = timePass - randDate
else:
finalTime = randDate - timePass
print(randDate, "is", finalTime, "away from", month,"-",day,"-",year,".")
def diffDate2(dateStr):
splits = dateStr.split("-")
day = int(splits[0])
print(day)
month = int(splits[1])
print(month)
year = int(splits[2])
print(year)
print(splits)
timePass = date(year, month, day)
if (timePass > randDate):
finalTime = timePass - randDate
else:
finalTime = randDate - timePass
print(finalTime)
inputDate()
# expected output shown below (or something similar)
# Input a day
# 13
# Input a month
# 7
# Input a year
# 2010
# user day: 07/13/2010
# random day: 09/11/2010
# The number of days between the given range of dates is: 60