Python core Programming chapter sixth Exercise 6-15

Source: Internet
Author: User

Transformation.
(a) give two recognizable dates, such as MM/DD/YY or DD/MM/YY format. Calculates the number of days between two dates.
(b) Give a person's birthday, counting the number of days from birth to the present, including all leap month.
(c) Also the above example, which calculates how many days the person's next birthday will be.


Answer
(a) The code is as follows:

def date_convert (date_input):    month = Int (date_input.split ('/') [0])    Day   = Int (date_input.split ('/') [1] Year  = Int. (' + ' + date_input.split ('/') [2])    return (year, month, day) import datetimedate_input = Raw_ Input (' Please input the begin date, mm/dd/yy ... ') D1 = datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) Date_ input = raw_input (' Please input the end date, mm/dd/yy ... ') D2 = Datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print ( D2-D1). Days

  

(b) The code is as follows:

def date_convert (date_input):    month = Int (date_input.split ('/') [0])    Day   = Int (date_input.split ('/') [1] Year  = Int (date_input.split ('/') [2])    return (year, month, day) Import datetimedate_input = Raw_input (' Please input his birthday, mm/dd/yyyy ... ') D1 = datetime.date (Date_convert (Date_input) [0], Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print ( Datetime.date.today ()-D1). Days

  


(c) The code is as follows:

def date_convert (date_input):    month = Int (date_input.split ('/') [0])    Day   = Int (date_input.split ('/') [1] Year  = Int (date_input.split ('/') [2])    return (year, month, day) Import datetimedate_input = Raw_input (' Please input his birthday, mm/dd/yyyy ... ') Import timenext_year = Int (time.strftime ('%Y ', Time.localtime (Time.time ()))) + 1next_birthday = datetime.date (next_ Year, Date_convert (Date_input) [1], Date_convert (Date_input) [2]) print (Next_birthday-datetime.date.today ()). Days

The above code support 06/13/15 does not support 06/13/2015

Python core Programming chapter sixth Exercise 6-15

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.