1

I would like to write a program that gives the day of the week on the 30th of the month. I have a code in which I have to specify the date, what can I change in it to work specifically for the 30th of the month?

import datetime
date=str(input('(Date for example 30 03 2019):'))
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%d %m %Y').weekday()
print(day_name[day])
El3na
  • 31
  • 3

1 Answers1

1

try this:

from datetime import datetime
year = int(input("Please enter year:"))
month = int(input("Please enter month:"))
try:
    print(datetime(year,month,30).strftime("%A"))
except ValueError as ex:
    # in February there is no 30 days
    print(f"No such date - {year}-{month}-30")

Keep in mind that you have to think about another logic for February...

Gabio
  • 9,126
  • 3
  • 12
  • 32
  • @MachineLearner I wrapped the code inside `try/except` block and mentioned in my solution that the OP should think about this case. Please read carefully... – Gabio Apr 16 '20 at 11:11