美文网首页
April 26th_day10_homework

April 26th_day10_homework

作者: 3ae1c50960af | 来源:发表于2019-04-29 09:41 被阅读0次

    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()
    
    

    相关文章

      网友评论

          本文标题:April 26th_day10_homework

          本文链接:https://www.haomeiwen.com/subject/dgnrnqtx.html