Exercise 7.2 (date.py, horoscope.py). First write the program date.py, which sho
ID: 3698470 • Letter: E
Question
Exercise 7.2 (date.py, horoscope.py). First write the program date.py, which should take in a month, day of the month and year, and convert it to the corresponding day of the year. Assume that leap years exist. Check that each date is correct (ie. March 56, 2017 is incorrect). For incorrect dates the conversion function should return -1, and you should print an error from main. For both programs, use dictionaries to store whatever data you might need (the number of days in each month, etc.) For example: python3 date2.py Type stop or enter a month, day, and year. >March, 14, 2015 4 Day of the year: 73 December, 14, 2016 s Day of the leap year: 349 7> Oktober, 91, 2015 s Invalid date entered. 9> stop Use the following boolean expression to check if a year is a leap year or not: 16Explanation / Answer
As per the given data wrote a program as shown below
Program:
import datetime
y =input("type stop or enter a month,date,year:")
month,date,year=y.split(",")
isValidDate = True
try :
datetime.datetime(int(year),int(month),int(day))
except ValueError :
isValidDate = False
if(isValidDate) :
print ("Input date is valid ..")
elseif(year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
print ("leap year")
else:
print("not a valid date")
2)horoscope.py
import datetime
y =input("type stop or enter a month,date,year:")
month,date,year=y.split(",")
isValidDate = True
try :
datetime.datetime(int(year),int(month),int(day))
except ValueError :
isValidDate = False
if(isValidDate) :
if month == 'december':
astro_sign = 'Sagittarius:today is hjkk' if (day < 22) else 'capricorn'
elif month == 'january':
astro_sign = 'Capricorn:today is hjkk' if (day < 20) else 'aquarius'
elif month == 'february':
astro_sign = 'Aquarius:today is hjkk' if (day < 19) else 'pisces'
elif month == 'march':
astro_sign = 'Pisces:today is hjkk' if (day < 21) else 'aries'
elif month == 'april':
astro_sign = 'Aries:today is hjkk' if (day < 20) else 'taurus'
elif month == 'may':
astro_sign = 'Taurus:today is hjkk' if (day < 21) else 'gemini'
elif month == 'june':
astro_sign = 'Gemini:today is hjkk' if (day < 21) else 'cancer'
elif month == 'july':
astro_sign = 'Cancer:today is hjkk' if (day < 23) else 'leo'
elif month == 'august':
astro_sign = 'Leo:today is hjkk' if (day < 23) else 'virgo'
elif month == 'september':
astro_sign = 'Virgo:today is hjkk' if (day < 23) else 'libra'
elif month == 'october':
astro_sign = 'Libra:today is hjkk' if (day < 23) else 'scorpio'
elif month == 'november':
astro_sign = 'scorpio:today is hjkk' if (day < 22) else 'sagittarius'
print(astro_sign)
else :
print("entered date is not valid")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.