Rattlesnake 1

From Grundy
Jump to: navigation, search

Date: 1st October 2017

Youtube Problem Statement: https://www.youtube.com/watch?v=owsqKkR2Bc0&t=10s

Youtube Solution: https://www.youtube.com/watch?v=Wzqknem7umY


Problem Statement:

Write a program in python to convert a given date to a day. The program should run in a loop and should keep asking if the user wants to quit. If the user wants to continue, he should be prompted to enter the date he wants to convert again.

Input format: You can choose any input format you want. However preferably it should be DD/MM/YYYY.

Output Format: The output should be the weekday, like "Monday", "Saturday" etc.

Example:

Enter a date to convert (DD/MM/YYYY):

- 14/01/2017

Saturday

Do you want to quit? (Y/N)

- Y

Solution:

 1     import datetime
 2     def convert(day, month, year):
 3         date = datetime.datetime.strptime(month + " " + day + ", " + year, "%m %d, %Y")
 4         return date.strftime('%A')
 5     while True:
 6         print "Enter a date in the DD/MM/YYYY format"
 7         date = raw_input()
 8         date_arr = date.split("/")
 9         day = convert(date_arr[0], date_arr[1], date_arr[2])
10         print "The day of the week is " + day
11         option = raw_input("Do you wish to quit? (Y/N)")
12         if option == "Y":
13         	break


See also