Discuss / Python / 练习:请把下面的Student对象的gender字段对外隐藏起来,用get_gender()和set_gender()代替,并检查参数有效性:

练习:请把下面的Student对象的gender字段对外隐藏起来,用get_gender()和set_gender()代替,并检查参数有效性:

Topic source

#1 Created at ... [Delete] [Delete and Lock User]

class Student(object):

    def __init__(self, name, gender):

        self.name = name

        self.__gender = gender

    def get_gender(self):

        return self.__gender

    def set_gender(self,gender):

        if gender == 'male' or 'female':

            self.__gender = gender

        else:

            raise ValueError('Error gender')

验证失败,当输入不是 male 或者 female  无法返回错误

而是输入什么就返回什么

Promethues

#3 Created at ... [Delete] [Delete and Lock User]

if gender == 'male' or 'female':

这个if判断永远为真。


  • 1

Reply