April 26th_day10_homework

question1. 写一个匿名函数,判断指定的年是否是闰年

year = lambda x: '是闰年' if (x % 4 == 0 and x % 100 != 0) or x % 400 == 0 else '不是闰年'

print(year(1900))
print(year(1992))
print(year(2000))
"""
不是闰年
是闰年
是闰年
"""

question2. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)

def lis_to_reverse(lis1: list):
    y = -1
    lis2 = []
    for x in range(len(lis1)):
        lis2.append(lis1[y])
        y -= 1
    return lis2


print(lis_to_reverse([1, 2, 3, 4, 'b', 'd', 'asd']))
"""
['asd', 'd', 'b', 4, 3, 2, 1]
"""

question3. 写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)

例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3

def lis_to_index(lis1: list, num1: int):
    lis2 = []
    for index in range(len(lis1)):
        if lis1[index] == num1:
            lis2.append(index)
    return lis2


print(lis_to_index([1, 3, 4, 1], 1))
"""
[0, 3]
"""

question4. 写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)

def dic_to_dic(dict1={}, dict2={}):
    for key in dict2:
        if key not in dict1:
            dict1[key] = dict2[key]
        else:
            continue
    return dict1


a = {'a': 1, 'b': 2, 'c': 3}
b = {'a': 1, 'b': 2, 'd': 3}
print(dic_to_dic(a, b))
"""
{'a': 1, 'b': 2, 'c': 3, 'd': 3}
"""

qusetion5. 写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母转换成小写字母(不能使用字符串相关方法)

def change_case(str1: str):
    str2 = ''
    for value in str1[:]:
        if 'a' <= value <= 'z':
            str2 += chr(ord(value) - 32)
        elif 'A' <= value <= 'Z':
            str2 += chr(ord(value) + 32)
        else:
            str2 += value
    return str2


print(change_case('abc123DEF'))
"""
ABC123def
"""

question6. 实现一个属于自己的items方法,可以将自定的字典转换成列表。列表中的元素是小的列表,里面是key和value(不能使用字典的items方法)

例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]]

def dic_to_list(dicts={}):
    list1 = []
    for key in dicts:
        list1.append([key, dicts[key]])
    return list1


a = {'a': 1, 'b': 2, 'c': 3}
b = dic_to_list(a)
print(b)
"""
[['a', 1], ['b', 2], ['c', 3]]
"""

question7. 写一个函数,实现学生的添加功能:



# 学生信息函数
def student_information():
    print('=============================')
    print('\t💣💣欢迎XXX💣💣')
    print('')
    print('\t🏀1.  添加学生')
    print('\t⚽️2.  查看学生')
    print('\t🏐️3.  修改学生信息')
    print('\t🏓️4.  删除学生')
    print('\t🏸️5.  返回')
    print('=============================')
    return input('请选择(1-5):')


# 添加学生函数
def add_student():
    global num_id
    while True:
        stu_num = 'python1902' + str(num_id).zfill(4)
        num_id += 1
        stu_id = stu_num
        name = input('请输入学生姓名:')
        age = int(input('请输入学生年龄:'))
        grade = int(input('请输入学生成绩:'))
        cellphone = input('请输入学生电话:')
        dic_of_stu_message[stu_num] = {'stu_id': stu_id,
                                       'name': name,
                                       'age': age,
                                       'grade': grade,
                                       'cellphone': cellphone
                                       }
        print('学生学号为:%s\t姓名:%s\t年龄:%d\t成绩:%d\t电话:%s' % (dic_of_stu_message[stu_num]['stu_id'],
                                                        dic_of_stu_message[stu_num]['name'],
                                                        dic_of_stu_message[stu_num]['age'],
                                                        dic_of_stu_message[stu_num]['grade'],
                                                        dic_of_stu_message[stu_num]['cellphone'])
              )
        print('添加成功!')
        choice = input('是否继续添加学生信息(yes/no):')
        if choice.lower() == 'no':
            break


# 删除学生函数-学号
def delete_stu_id():
    temp_stu_id = input('请输入需要删除的学生学号:')
    print('删除学生学号为:%s\t姓名:%s\t年龄:%d\t成绩:%d\t电话:%s' % (dic_of_stu_message[temp_stu_id]['stu_id'],
                                                        dic_of_stu_message[temp_stu_id]['name'],
                                                        dic_of_stu_message[temp_stu_id]['age'],
                                                        dic_of_stu_message[temp_stu_id]['grade'],
                                                        dic_of_stu_message[temp_stu_id]['cellphone'])
          )
    temp = input('确认删除?(yes/no):')
    if temp == 'yes':
        del dic_of_stu_message[temp_stu_id]
        print('删除成功')


# 查询学生函数
def check_student():
    print('=============================')
    print('\t1.查看所有学生')
    print('\t2.按姓名查找')
    print('\t3.按学号查找')
    print('\t4.返回')
    return input('请选择(1-4):')


# 打印所有学生
def print_all_student():
    for index in dic_of_stu_message:
        print('学生学号为:%s\t姓名:%s\t年龄:%d\t成绩:%d\t电话:%s' % (dic_of_stu_message[index]['stu_id'],
                                                        dic_of_stu_message[index]['name'],
                                                        dic_of_stu_message[index]['age'],
                                                        dic_of_stu_message[index]['grade'],
                                                        dic_of_stu_message[index]['cellphone'])
              )


# 冒泡排序(列表中字典-按成绩排序)
def bubble_sort(lis):
    length = len(lis)
    for i in range(1, length):
        for j in range(0, length-1):
            if lis[j]['grade'] < lis[j+1]['grade']:
                lis[j], lis[j+1] = lis[j+1], lis[j]
    return lis


# 读入以储存学生信息
dic_of_stu_message = {}
filename = 'student_information.txt'
list_of_test1 = []

with open(filename, 'r') as file_object:
    line = (x for x in file_object)
    num_id = 1
    while True:
        stu_num = 'python1902' + str(num_id).zfill(4)
        num_id += 1
        try:
            dic_of_stu_message[stu_num] = {'stu_id': next(line).rstrip(),
                                           'name': next(line).rstrip(),
                                           'age': int(next(line).rstrip()),
                                           'grade': int(next(line).rstrip()),
                                           'cellphone': next(line).rstrip()
                                           }
        except StopIteration:
            break
print(dic_of_stu_message)
file_object.close()

# 主程序
num_id = len(dic_of_stu_message) + 1
print(num_id)
while True:
    temp1 = student_information()
    if temp1 == '1':
        add_student()
        continue
    elif temp1 == '2':
        temp2 = check_student()
        if temp2 == '1':
            print_all_student()
        elif temp2 == '4':
            continue
    elif temp1 == '4':
        delete_stu_id()
    elif temp1 == '5':
        break

# 主程序执行完成后存储学生信息
with open(filename, 'w') as file_object:
    for key1 in dic_of_stu_message:
        for key2 in dic_of_stu_message[key1]:
            file_object.write(str(dic_of_stu_message[key1][key2])+'\n')
file_object.close()

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容