Python Exercise Example 4
Python Exercise Example 4
Title: Enter a year on a certain day, judgment day is the day of the year?
Program analysis: March 5, for example, should be put before the two months together, then add five days or the first few days of this year, special circumstances, leap year and the input of more than 3 months to pay more to consider when one day :
Source Code:
Examples (Python 2.0+)
#! / usr / bin / python
# - * - Coding: UTF- 8 - * -
year = int (raw_input ( 'year : \ n'))
month = int (raw_input ( 'month : \ n'))
day = int (raw_input ( 'day : \ n'))
months = (0, 31, 59 , 90, 120, 151, 181, 212, 243, 273, 304, 334)
if 0 <month <= 12: sum = months [month - 1]
else: print 'Data error'
sum + = day
leap = 0
if (Year% 400 == 0) or ((Year% 4 == 0) and (Year% 100 = 0!) ): Leap = 1
if (Leap == 1) and (Month> 2): sum + = 1
print 'It is the% dth day. '% Sum
The above example output is:
year: 2015 month: 6 day: 7 it is the 158th day.