Day11-homework

  1. 写一个匿名函数,判断指定的年是否是闰年
years = lambda year: year % 400 == 0 or (year % 100 != 0 and year % 4 == 0)
year = int(input('请输入一个年份'))
print(years(year))
  1. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def sort_cc(s):
    s = s[::-1]
    return s

print(sort_cc([1,2,3]))
  1. 写一个函数,获取指定列表中指定元素的下标(如果指定元素有多个,将每个元素的下标都返回)
    例如: 列表是:[1, 3, 4, 1] ,元素是1, 返回:0,3
def find_cc(list1:list,item):
    list2 = []
    for index in range(len(list1)):
        if string[index] == item:
            list2.append(index)
     return list2
  1. 写一个函数,能够将一个字典中的键值对添加到另外一个字典中(不使用字典自带的update方法)
def update_cc(dict1:dict,dict2:dict):
    for key in dict2:
        dict1[key] = dict2[key]
    return dict1
  1. 写一个函数,能够将指定字符串中的所有的小写字母转换成大写字母;所有的大写字母转换成小写字母(不能使用字符串相关方法)
def exchange_cc(string:str):
    new_str = ''
    for item in string:
        if 'a' <= item <= 'z':
            item = chr(ord(item) - 32)
            new_str += item
        elif 'A' <= item <= 'Z':
            item = chr(ord(item) + 32)
            new_str += item
        else:
            new_str += item
    return new_str
  1. 实现一个属于自己的items方法,可以将自定的字典转换成列表。列表中的元素是小的列表,里面是key和value (不能使用字典的items方法)
    例如:{'a':1, 'b':2} 转换成 [['a', 1], ['b', 2]]
def items_cc(dict_old):
    list_new = []
    for key in dict_old:
        list1 = []
        list1.append(key)
        list1.append(dict_old[key])
        list_new.append(list1)

    return list_new
  1. 写一个函数,实现学生的添加功能:
student = {}
students = []


def add(name, age, tel):
    count = 1
    s = ''
    n = 0
    while count == 1:
        print('==========添加学生============')
        name = input('请输入学生姓名')
        age = int(input('请输入学生年龄'))
        tel = input('请输入学生电话')
        n += 1
        s = str(n)
        c = ''
        number = '000000'
        for x in range(len(number)-len(s)):
            c += number[x]
        c = c + s
        student = {'name': name, 'age': age, 'tel': tel, '学号': c}
        print('==========添加成功!===========')
        students.append(student)
        print(students)
        print('=============================')
        print('1.继续')
        print('2.返回')
        count = int(input('请选择'))
    print('============添加结束=======')


add('name', 'age', 'tel')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。