1

Q:

Write a program called birthday_021.py that reads lines of text. where each line consists of a person’s date of birth. A date of birth is specified by three integers: a day, a month and a year. The program should determine on which day of the week each person was born and print the corresponding line from the poem:

Monday’s child is fair of face Tuesday’s child is full of grace

Wednesday’s child is full of woe Thursday’s child has far to go

Friday’s child is loving and giving Saturday’s child works hard for a

living Sunday’s child is fair and wise and good in every way For

My code:

#!/usr/bin/env python3

import datetime 
import calendar 
  
def findDay(date): 
    born = datetime.datetime.strptime(date, '%d %m %Y').weekday() 
    return (calendar.day_name[born]) 

date = '26 11 2001'
print("You were born on a " + findDay(date))

My problem:

In the code I posted above it prints "you were born a Friday".

The problem is I Don't know how to add the poem line for Friday to the output. My answer should be "you were born on a Friday and Friday's child is loving and giving"

2
  • Have you tried using dictionary with key as weekday?
    – Girish
    Commented Jan 30, 2021 at 12:54
  • Create a dictionary that would have keys "Monday", "Tuesday", etc and values - the corresponding poem lines. Then add to print your_dicitonary[findDay(date)] Commented Jan 30, 2021 at 12:56

1 Answer 1

3

Use a dictionary where the day of the week is the key, and the value is the line of the poem to print.

import datetime
import calendar


def findDay(date):
    born = datetime.datetime.strptime(date, '%d %m %Y').weekday()
    return (calendar.day_name[born])


lines = {"Monday": "Monday’s child is fair of face",
         "Tuesday": "Tuesday’s child is full of grace",
         # Fill in the rest of the rows...
         }


date = '20 11 2001'
print(lines[findDay(date)])

Or, if you want to print the sentence with the day as well

day = findDay(date)
print("You were born on a " + day + ". " + lines[day])
1
  • but how can I print "you were born (day) + poem line. All in one line e.g "you were born a Monday and Monday's child is fair of face" Commented Jan 30, 2021 at 13:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.