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.
import datetime
import random
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,
}
usr_date = input("Enter a date MM/DD/YYYY").split("/")
usr_day, usr_month, usr_year = int(usr_date[0]), int(usr_date[1]), int(usr_date[2])
def generateRandomDate(year):
start = datetime.datetime(year, 1, 1)
return start + datetime.timedelta(days=random.randint(1,364))
def calculateDistance(m1,d1,m2, d2):
days = 0
if m1 == m2:
return d2-d1
days += days_dictionary[m1] - d1
month = m1 + 1
while month != m2:
days += days_dictionary[month]
month += 1
days += d2
return days
random_date = generateRandomDate(usr_year)
random_month=random_date.month
random_day=random_date.day
if (random_month > usr_month or (random_month == usr_month and random_day > usr_day)):
print("There are {0} days between your date of {1}/{2} and the random date of {3}/{4}".format(calculateDistance(usr_month, usr_day, random_month, random_day), usr_month, usr_day, random_month, random_day))
else:
print("There are {0} days between your date of {1}/{2} and the random date of {3}/{4}".format(calculateDistance(random_month, random_day, usr_month, usr_day), usr_month, usr_day, random_month, random_day))
def DecimalToHex(num):
hex_correlation = {
0:"0",
1:"1",
2:"2",
3:"3",
4:"4",
5:"5",
6:"6",
7:"7",
8:"8",
9:"9",
10:"A",
11:"B",
12:"C",
13:"D",
14:"E",
15:"F"
}
res = ""
digits = 7
while digits >= 0:
if (num - 16 ** digits) >= 0:
sub_res = 0
while (num - 16 ** digits) >= 0:
sub_res+=1
num-=16 ** digits
res = res + str(hex_correlation[sub_res])
else:
res = res + "0"
digits-=1
return res
num = random.randint(0,1000000000)
print("Random Number:", num)
print("Binary representation of random number:", bin(num)[2:])
print("Hexadecimal representation of random number:", DecimalToHex(num))