三天打鱼两天晒网Python+实现简单文件读取

        提前小声bb:正在努力学习代码规范中,一时之间可能还做不到很好,如果代码看着不顺眼请原谅QAQ

        如果只要代码请直接拉到最下面~如果有错误的地方麻烦提出来感激不尽XDDD


【问题描述】

        中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行判定,并将结果输出至out.txt文件

【问题分析】

        这道题其实就是求20100101到指定时间之间的天数也就是先算出2010到给定年份的前一年一共有多少天,然后加上给定月份的前一月的天数,最后加上给定的具体天数。例如给定时间为20120323,那么天数就等于2010到2011年(365*2)+1月到2月(31+28)+23这么多天。然后把天数总数除以5看余数,如果大于三为晒网,小于等于三为打渔。

        它的难点为(1)闰年平年天数不一样(2)每个月的天数不一样,而由于python list的存在使得月份不同这个问题很好解决。

        关于存储文件由于目前我学的python还不是很好quq所以只能实现保存在in.txt中,从in.txt中读取试了一下只能读取一行,迫不得已放弃了。

【难点解决】

       (1)求年份间天数总和

def GetYearsCount(year):   #get the days in years
    count=0;  
    if year>2010:
        for x in range(2010,year):
            if((x%4==0)and(x%400!=0)):  #judge leap year
                count=count+366  #is leap year
            else:
                count=count+365
        return count
    else:
        return count   

       (2)求月份间天数总和

def GetMonthsCount(month):  #get the days in months
    days=0
    Month_days=[31,28,31,30,31,30,31,31,30,31,30,31]  #twelve months
    if month==1:  
        return days
    if month==2:  #the second month of leap year has 29 days
        if ((Year%4==0)and(Year%400!=0)):
            Month_days[1]=29
        else:
            Month_days[1]=28
    for x in range(0,month-1 ):
        days=days+Month_days[x]  #calculate the days from January
                                  #to the before month
    return days

【流程图】

【代码】

           分为纠错版(年份月份日期分开输入实现纠错)和不纠错版(输入模板为20100101),虽然都是一样的菜QAQ

【纠错版】

'''
    Author:Innocence
    IDE:Python IDLE 3.7.0 Shell
    OS:win10
    Editor:1.0
    Time:2019/2/26
    Description:Given a date,judge "Fishing" or "Dying net",we can using the
     date to calculate how many days there are from 20100101 to the date ,and then
     mod the days,if the result is below 3,Dying net,if not ,Fishing.Need to pay
     attention to that the leap year has 366 days,while average year only has 365
     days.
'''

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def GetYearsCount(year):   #get the days in years
    count=0;
    if year>2010:
        for x in range(2010,year):
            if((x%4==0)and(x%400!=0)):  #judge leap year
                count=count+366  #is leap year
            else:
                count=count+365
        return count
    else:
        return count   
         
def GetMonthsCount(month):  #get the days in months
    days=0
    Month_days=[31,28,31,30,31,30,31,31,30,31,30,31]  #twelve months
    if month==1:  
        return days
    if month==2:  #the second month of leap year has 29 days
        if ((Year%4==0)and(Year%400!=0)):
            Month_days[1]=29
        else:
            Month_days[1]=28
    for x in range(0,month-1 ):
        days=days+Month_days[x]  #calculate the days from January
                                  #to the before month
    return days

fi=open('in.txt','a+')  #create fi
fo=open('out.txt','a+')  #create fo
print('Please enter the year:')
Year=int(input())  #input year
while Year<2010:
    print('The Year is worry,please check and input again:')
    Year=int(input())
print('Please enter the month:')
Month=int(input())  #input year
while Month<1 or Month>12:
    print('The Month is worry,please check and input again:')
    Month=int(input())
print('Please enter the day:')
Day=int(input())  #input year
while Day<1 or Day>31:
    print('The Day is worry,please check and input again:')
    Day=int(input())
fi.write('  ')
fi.write(str(Year)+str(Month)+str(Day))  #write time into fi
Sum=0  #the whole days
print('Year:',GetYearsCount(Year))  #used as a test
print('Month:',GetMonthsCount(Month))  #used as a test
print('Day:',Day)  #used as a test
Sum=GetYearsCount(Year)+GetMonthsCount(Month)+Day
print(Sum)  #used as a test
if(Sum%5>3):  #if left>3,Dying net
    fo.write('Dying net')
    fo.write('  ')
else:
    fo.write('Fishing')
    fo.write('  ')
fi.close()
fo.close()
        

【不纠错版】

'''
    Author:Innocence
    IDE:Python IDLE 3.7.0 Shell
    OS:win10
    Editor:1.0
    Time:2019/2/26
    Description:Given a date,judge "Fishing" or "Dying net",we can using the
     date to calculate how many days there are from 20100101 to the date ,and then
     mod the days,if the result is below 3,Dying net,if not ,Fishing.Need to pay
     attention to that the leap year has 366 days,while average year only has 365
     days.
'''

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def GetYearsCount(year):   #get the days in years
    count=0;
    if year>2010:
        for x in range(2010,year):
            if((x%4==0)and(x%400!=0)):  #judge leap year
                count=count+366  #is leap year
            else:
                count=count+365
        return count
    else:
        return count   
         
def GetMonthsCount(month):  #get the days in months
    days=0
    Month_days=[31,28,31,30,31,30,31,31,30,31,30,31]  #twelve
    if month==1:  
        return days
    if month==2:  #the second month of leap year has 29 days
        if ((Year%4==0)and(Year%400!=0)):
            Month_days[1]=29
        else:
            Month_days[1]=28
    for x in range(0,month-1 ):
        days=days+Month_days[x]  #calculate the days from January
                                  #to the before month
    return days

fi=open('in.txt','a+')  #create fi
fo=open('out.txt','a+')  #create fo
print('Please enter the date,the format is:20100101')
Time=input()  #input time
fi.write('  ')
fi.write(Time)  #write time into fi
Time=int(Time)  #transform time(str)into int
Year=int(Time/10000)   #Get year
Month=int(Time%10000/100)  #Get month
Day=int(Time%10000%100)  #Get day
Sum=0  #the whole days
print('Year:',GetYearsCount(Year))  #used as a test
print('Month:',GetMonthsCount(Month))  #used as a test
print('Day:',Day)  #used as a test
Sum=GetYearsCount(Year)+GetMonthsCount(Month)+Day
print(Sum)  #used as a test
if(Sum%5>3):  #if left>3,Dying net
    fo.write('Dying net')
    fo.write('  ')
else:
    fo.write('Fishing')
    fo.write('  ')
fi.close()
fo.close()
        

【心得】

        好久没用python了好多都忘了emmmm看来知识真的要经常复习然后多多使用。

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值