a = [] for i in range(5): if i == 0: name = input("请输入你的姓名:") else: name = input("请输入第{}个人的姓名:".format(i+1)) birth_year = input("请输入出生年份:") birth_month = input("请输入出生月份:") a.append(birth_year + birth_month + " " + name) print(a)

a.sort() print(a)

b1 = [] b2 = [] for i in a: birth_year = int(i[:4]) if birth_year == 2002: b1.append(i) elif birth_year == 2003: b2.append(i) print("2002年出生的人:", b1) print("2003年出生的人:", b2)

age_list = [] for i in a: birth_year = int(i[:4]) birth_month = int(i[4:6]) age = 2021 - birth_year - (7 - birth_month) // 12 age_list.append(age) min_index1 = age_list.index(min(age_list)) age_list.pop(min_index1) min_index2 = age_list.index(min(age_list)) print("年龄最小的两位舍友是:", a[min_index1], a[min_index2])

c = a.pop(0) print("已删除的人:", c) print("剩余的人:", a)

把你们自己宿舍的5个人的出生年月和姓名通过input函数输入添加进列表a里第一个输入的必须是自己然后打印出来;输入的时候要有文字提示输入的是第几个人列表格式举例:a=200803 Lanbipeng…2对列表a的元素进行排序并打印出来;3把同一年出生的列表元素归为一个列表比如2002年出生的赋给列表b12003年出生的赋给列表b2然后都打印出来;4把年龄最小的两位舍友自己也算打印出来要有文字提示;

原文地址: https://www.cveoy.top/t/topic/KIG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录