python练习题2

#给出一个日子,和一个数字(可以是正数或负数)
#算出这个日子加上这个天数后是哪个日子
def day():
    date=raw_input("(MM/DD/YY)>>")
    date=date.split("/")
    year1=int(date[-1])
    month1=int(date[0])
    day1=int(date[1])
    num=raw_input("enter a number:")
    num=int(num)
    month_days=[31,28,31,30,31,30,31,31,30,31,30,31]
    days1=sum(month_days[:(month1-1)])+day1
    
    if int(date[2])%4==0:
        month_days[3]=29
        years1=366
    else:
        years1=365
   
    sumdays=days1+num
    if num>0:
        lis=[]
        if sumdays<years1:
            year2=year1
            print year2
            lis.append(year2)
            sumday=0
            for i,j in enumerate(month_days):
                if sumday<sumdays:
                    sumday=sumday+int(j)
                else:
                    month2=i
                    print month2
                    lis.append(month2)
                    marginday=sum(month_days[:i])-sumdays
                    day2=month_days[i-1]-marginday
                    print day2
                    lis.append(day2)
                    break 
            print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])   
        elif sumdays>years1:
            lis=[]
            years2=years1
            year2=year1
            remindays=sumdays-years2
            while remindays>years2:
                year2=year2+1
                lis.append(year2)
                if year2%4==0:
                    remindays=remindays-366
                else:
                    remindays=remindays-365
            print remindays  

            for i,j in enumerate(month_days):
                if remindays]):
                    month2=i
                    lis.append(month2)
                    day2=remindays-sum(month_days[:i])
                    lis.append(day2)
                    break
                
            print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])
        else:
            print "%d天后的日期为:%d年%d月%d号"%(num,year1,12,31)
    #elif sumdays==0:
        
        #print "%d天后的日期为:%d年%d月%d号"%(num,year1,1,1)
    else:
        lis=[]
        remindays=0
        years2=years1
        year2=year1        
        if sumdays<0:
            
           
            #if year2%4==0:
                #years2=366
            #else:
                #years2=365        
            #remindays=abs(sumdays)-years2
            remindays=abs(sumdays)
            while remindays>0:
                year2=year2-1
                
                if year2%4==0:
                    remindays=remindays-366
                else:
                    remindays=remindays-365
            lis.append(year2)
            print remindays  
            remindays=abs(remindays)
        elif sumdays==0:
            print "%d天后的日期为:%d年%d月%d号"%(num,year1,1,1)
        else:
            remindays=sumdays
            lis.append(year1)
        print remindays
        for i,j in enumerate(month_days):
            if remindays]):
                month2=i+1
                lis.append(month2)
                day2=remindays-sum(month_days[:i])
                lis.append(day2)
                break
            
        print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2])        
        
     
    #lis=[]
    #for i,j in enumerate(month_days):
        #if i+1==int(date[0]):
            #day=month_days[i]-int(date[1])
            #if num+int(date[1])
                #lis.append(int(date[0]))
                #lis.append(num+int(date[1]))
                #lis.append(int(date[-1]))
    #print lis       
day()

 

转载于:https://www.cnblogs.com/sunyucui/p/7010983.html

你可能感兴趣的:(python练习题2)