PYTHON:根据出生年月日计算年龄并且分段(datatime空间里的datatime模块)

实验内容​ 

  1. 从网站下载python语言的开发环境,并安装。
  2. 设计一个数据结构存储全校学生的基本情况。
  3. 基本情况应该包括:学号,姓名(可能存在同名),性别,年龄
  4. 设计程序实现通过姓名查找学生。
  5. 可以统计每个年龄段的人数。

思路

       创建一个字典作为数据库, 一个键值对多个键值储存多个学生的信息。通过for循环匹配要查找的姓名与学生的姓名是否相同,是的话就输出用户指定的信息。判断学生的年龄是否处于指定的年龄段,是的话就使数组对应元素计数加一。

程序源代码

import datetime
people = {
'1235':{
    'name':'Alice',
    'sex':'girl',
    'age':'2012-09-15'
    },
'1234':{
    'name':'Alice',
    'sex':'girl',
    'age':'1997-11-08'
    },
'5628':{
    'name':'Beth',
    'sex':'boy',
    'age':'1882-03-03'
     },
'5728':{
    'name':'Beth',
    'sex':'boy',
    'age':'1990-01-05'
     },
'4785':{
    'name':'Cecil',
    'sex':'girl',
    'age':'2015-03-10'
     }
}

labels = {
    'num':'student number',
    'name':'student name',
    'sex':'student sex',
    'age':'student age'#出生年月日,当前系统系统时间计算年龄
}

def find():
    student_name=input('Name:')
    for key in people:
        if people[key]['name']==student_name:
            request = input('find :num(n) or sex (s) or age (a)?')#重名会查找多次
            if request == 'n':
                choose_key = 'num'
                print("{}'s {} is {}.".format(student_name,labels[choose_key],key))              
            if request == 's':
                choose_key = 'sex'
                print("{}'s {} is {}.".format(student_name,labels[choose_key],people[key][choose_key]))
            if request == 'a':
                choose_key = 'age'
                birth_str=people[key][choose_key] #计算年龄
                birth = datetime.datetime.strptime(birth_str, '%Y-%m-%d')
                now = datetime.datetime.now().strftime('%Y-%m-%d')                
                now = datetime.datetime.strptime(now,'%Y-%m-%d')
                if now.month < birth.month:
                    age = now.year-birth.year-1
                if now.month > birth.month:
                    age = now.year-birth.year
                if now.month == birth.month and now.day < birth.day:
                    age = now.year-birth.year-1
                if now.month == birth.month and now.day > birth.day:
                    age = now.year-birth.year    

                print("{}'s {} is {}.".format(student_name,labels[choose_key],age))
            

def count():
    age_matrix=[0]*6 #1-5,6-10,11-15,16-20,21-25,other    
    for key in people:
        birth_str=people[key]['age']
        birth = datetime.datetime.strptime(birth_str, '%Y-%m-%d')
        now = datetime.datetime.now().strftime('%Y-%m-%d')                
        now = datetime.datetime.strptime(now,'%Y-%m-%d')
        if now.month < birth.month:
            age = now.year-birth.year-1
        if now.month > birth.month:
            age = now.year-birth.year
        if now.month == birth.month and now.day < birth.day:
            age = now.year-birth.year-1
        if now.month == birth.month and now.day > birth.day:
             age = now.year-birth.year
             
        if age>=1 and age<=5:
            age_matrix[0]=age_matrix[0]+1
        if age>=6 and age<=10:
            age_matrix[1]=age_matrix[1]+1
        if age>=11 and age<=15:
            age_matrix[2]=age_matrix[2]+1
        if age>=16 and age<=20:
            age_matrix[3]=age_matrix[3]+1
        if age>=21 and age<=25:
            age_matrix[4]=age_matrix[4]+1
        if age>=26:
            age_matrix[5]=age_matrix[5]+1
    print(("{}年龄段:{}人,{}年龄段:{}人,{}年龄段:{}人,{}年龄段:{}人,{}年龄段:{}人,{}年龄段:{}人.".
          format('1-5',age_matrix[0],'6-10',age_matrix[1],'11-15',age_matrix[2],'16-20',age_matrix[3],'21-25',age_matrix[4],'other',age_matrix[5])))

             
def main():
    flag = 'c'
    print(people)
    while flag == 'c':
        find()
        count()
        flag = input('continue (c) or stop (s)')

main()

上面源程序里的时间模块

birth_str=people[key][choose_key] #获取字典里学生的出生日期
birth = datetime.datetime.strptime(birth_str, '%Y-%m-%d') #用-分割学生的出生日期,获取年月日
now = datetime.datetime.now().strftime('%Y-%m-%d') #获取今天的日期                
now = datetime.datetime.strptime(now,'%Y-%m-%d')    #分割今天的日期获取年月日
if now.month < birth.month: #如果学生月份比今天大,他肯定没过生日,则年份相减再减一
   age = now.year-birth.year-1
if now.month > birth.month:    #如果学生月份比今天小,他过生日了,则年份相减
   age = now.year-birth.year
if now.month == birth.month and now.day < birth.day: #如果月份相等,学生日比今天大,他没过生日
   age = now.year-birth.year-1
if now.month == birth.month and now.day > birth.day: #如果月份相等,学生日比今天小,他过生日了
   age = now.year-birth.year    

运行截图

PYTHON:根据出生年月日计算年龄并且分段(datatime空间里的datatime模块)_第1张图片

你可能感兴趣的:(python笔记)