学了5天的作业

# 写一个程序,判断2008年是否是闰年。
# 普通年(不能被100整除的年份)能被4整除的为闰年。(如2004年就是闰年,1999年不是闰年);
# 世纪年(能被100整除的年份)能被400整除的是闰年。(如2000年是闰年,1900年不是闰年);

def leap_year(year):
    L = []
    if(year%4==0 ):
        a = True
        L.append(a)
    elif(year%400==0):
        b = True
        L.append(b)
    if(any(L)):#any()任意一个是true就是true
        print(year,'是闰年')
    else:
        print(year, '不是闰年')

leap_year(2000)


# 写一个程序,用于计算2008年10月1日是这一年的第几天?(2008年1月1日是这一年的第一天)
#
#解题 思路:先把12个月的天数存一个数组中,1、3、5、7、8、10、12月为31天,2月份平年有28天,闰年有29天要先得出来,其它剩下的都是30天,,
# 几月份几号是第几天,判断出之前的天数,再加上当月的几日这几天就行了。


def which_day(year, month, day):
    L = [31, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    #第几天
    allday=0
    #先把2月份的天数算出来,放在L的第二位上
    if(year%4==0):
        L.insert(1,29)
    elif(year%400==0):
        L.insert(1,29)
    else:
        L.insert(1,28)
    print('L==',L)
    #几月份几号是第几天,判断出之前的天数,再加上当月的几日这几天就行了

    for i in range(month-1):
        print('第',i+1,'个月的天数是:',L[i])
        allday +=L[i]

    print(allday)
    print(year,'年',month,'月',day,'日是这一年中的第',allday,'天!')


which_day(2008,10,1)


# 有一个record.txt的文档,内容如下:
# name, age, score
# tom, 12, 86
# Lee, 15, 99
# Lucy, 11, 58
# Joseph, 19, 56
# 第一栏为姓名(name),第二栏为年纪(age),第三栏为得分(score)
# 现在,写一个Python程序,
# 1)读取文件
# 2)打印如下结果:
# 得分低于60的人都有谁?
# 谁的名字以L开头?
# 所有人的总分是多少?
# 3)姓名的首字母需要大写,该record.txt是否符合此要求? 如何纠正错误的地方?
#解题思路:age没用到啊,那么读取文件把一行中的名字和分数存为一个dict就好办多了
def get_info():
    f = open('mingan.txt','r')
    # print(f.readlines())
    txtlist = f.readlines() #生成列表list
    txtlist.pop(0) #删除标题第1行
    # print(txtlist)
    dic={}
    for i in txtlist:

        # print(i.split(',')[0])
        # 首字母是否大写
        if (i.split(',')[0].istitle()):
            i.split(',')[0].capitalize()


        # 按逗号分隔,取左右2列字段
        dic[i.split(',')[0]] = i.split(',')[2] #给字典中添加元素

    # print(dic)
    #计算低于60分的人
    low_l =[]
    #统计首字母为L的人
    L_name=[]
    #统计所有人的总分
    sum_score=0
    for key in dic:
        # print(type(dic[key]))
        if(int(dic[key]) < 60):#要把字符串转为int才能比较
            low_l.append(key)
        #谁的名字以L开头
        if(key[0:1] =='L'):
            L_name.append(key)
        # 判断是否所有姓名首字母都是大写
        if( str(key).istitle()):
            str(key).capitalize()
        #计算总分
        sum_score +=int(dic[key])

    print('低于60分的人有:',low_l)
    print('谁的名字以L开头:',L_name)
    print('所有人的总分是:',sum_score)
get_info()

 

posted @ 2018-02-24 17:54  sincoolvip  阅读(143)  评论(0编辑  收藏  举报