In: Computer Science
Modify this python program to print "Happy Birthday!" if today is the person's birthday. Remember that a person has a birthday every year. It is not likely that a newborn will wonder into a bar the day s/he is born. Your program should print "Happy Birthday!" if today's month and day are the same as the person's birthday. For example, if person is born November 3, 1984 and today is November 3, 2019, then a happy birthday message should print.
The code:
import datetime from dateutil.relativedelta import relativedelta today = datetime.date.today() today """from .. import * considered bad form.""" # from datetime import * """This is better style.""" from datetime import date today type(today) today.month today.day #@title Enter birthday bday_input = "1995-06-15" #@param {type:"date"} bday_input = input('Enter your birthday: ') bday_input bday_comps = bday_input.split('-') year = bday_comps[0] month = bday_comps[1] day = bday_comps[2] print(month, day, year) type(year) year, month, day = bday_input.split('-') print(month, day, year) type(year) bday = datetime.date(int(year), int(month), int(day)) bday diff = relativedelta(today, bday) diff diff.years if diff.years >= 21: print('Welcome!') print('Stamp hand.') else: print('Piss off kid.') print('Next!')
From your given program, I have removed insignificant libraries and modified the program to give your required output. The program will be required to input the birthday date in the exact way asked (as per your given code)
Here is the code:
import datetime
from datetime import date #imported required
library
today = datetime.datetime.today() #today will
consist the data of the current date
today_month=today.month
today_day=today.day
bday_input = "1995-06-15" #@param {type:"date"} #taking
birthday input
bday_input = input('Enter your birthday: ')
bday_comps = bday_input.split('-') #separating
the input into three parts-day,month,year
year = int(bday_comps[0]) #typecasting into "int"
month = int(bday_comps[1])
day = int(bday_comps[2])
if month==today_month and day==today_day: #comparison of the
input birthday date with today's date
print("Happy Birthday !") #if the birthday date and today's
date matches, it prints "HappyBirthday"
else:
print("Hey, It's not your birthday today")