adds和updates的运行结果问题

adds和updates的运行结果问题

def adds(self, new_students):
    for student in new_students:
        try:
            self.check_user_info(**student)
        except Exception as e:
            print(e, student.get('name'))
            continue
        self.__add(**student)
        
def updates(self, update_students):
    for student in update_students:
        try:
            id_ = list(student.keys())[0]  
        except IndexError as e:          
            print(e)
            continue
        if id_ not in self.students:  
            print('并不存在学号{}'.format(id_))
            continue

        try:
            self.check_user_info(**student[id_])  
        except Exception as e:
            print(e)
            continue

        self.students[id_] = student[id_]
    print('更新完成')
    
def check_user_info(self, **kwargs):
    assert len(kwargs) == 4, '参数必须是4个'

    if 'name' not in kwargs:
        raise NotArgError('缺少学生姓名参数')
    if 'age' not in kwargs:
        raise NotArgError('缺少学生年龄参数')
    if 'class_' not in kwargs:
        raise NotArgError('缺少学生班级参数')
    assert 'sex' in kwargs, '缺少学生性别参数'

    name_value = kwargs['name']
    age_value = kwargs['age']
    class_value = kwargs['class_']
    sex_value = kwargs['sex']
    if not isinstance(name_value, str):
        raise TypeError('name应该是字符串类型')
    assert isinstance(age_value, int), 'age是整型'
    if not isinstance(class_value, str):
        raise TypeError('class_是字符串类型')
    if not isinstance(sex_value, str):
        raise TypeError('sex是字符串类型')
        
users = [
    {'name': 'huahua', 'age': 2, 'class_': 'A'},
    {'name': 'xiaoxin', 'age': 6, 'class_': 'C', 'sex': 'boy'},
    {'name': 'heye', 'age': 2, 'sex': 'boy'}
]       
student_info.adds(users)

updates = [
    {1: {'name': 'dewei.Zhang', 'age': 33, 'class_': 'B', 'sex': 'boy'}},
    {3: {'name': 'xiaoman', 'age': '30'}},
    {9: {'name': 'xiaogao', 'age': 20, 'class_': 'B', 'sex': 'boy'}}
]
student_info.updates(updates)

结果:
参数必须是4个 huahua
参数必须是4个 heye

参数必须是4个
并不存在学号9
更新完成
  1. printe(e)返回的结果只有‘参数必须是4个’,不会返回后面这些信息了。我的理解是python执行到第一个异常的时候,就会停止,不再继续执行了(就想try也不会执行错误后面的指令)。那后面这些指令似乎就没有意义了。怎么可以修改呢?

  2. check_user_info里,raise的语句是不是也可以用assert代替。只要是判断语句就可以用assert,不用raise?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小猿 2022-06-14 17:53:47

同学,你好!

1.是继续执行的,因为try..... except.....在for 循环里,第一次循环执行后,会继续执行第二次循环。同学说的'错误后面的指令'指的是哪个位置的代码呢?同学可以提供下截图

2.check_user_info里可以用assert代替,如图

https://img1.sycdn.imooc.com//climg/62a85a9209a0c2f906040136.jpg

较为复杂的if...... else......语句不适合用assert

祝学习愉快~


  • 提问者 呆呆桃 #1

    for循环循环的是列表里面的每个成员。我的意思是check_user_info这个函数,如果assert的这个错误抛出了,下面的if语句好像都没有再执行了,这些语句似乎就没有意义了。



    2022-06-15 10:06:26
  • 好帮手慕小猿 回复 提问者 呆呆桃 #2

    同学,你好!程序是由上到下顺序执行assert 抛出错误后边的代码是不再继续执行了,如果没有assert抛出错误,他会继续执行后边的语句进行判断是有用的,祝学习愉快~

    2022-06-15 10:55:48
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师