跟着书上例子学习
1.判断输入的名字---------(没问题)
2.判断输入的学校------------(没问题)
3.判断输入的班级 是否在设定之内 -----可以就打印输入---(没问题)
-----但是输出的值超范围就报错 我设定的班级是['k','1','2','3','4','5']
代码截图如下:
控制台显示如下 正确输入没问题 :
但是控制台输出超出我预设的范围就报错:
class Student(object): def __init__(self, name="", school="", grade=""): if not name: name = raw_input('What is the student \'s name?') if not school: school = raw_input('What is the student \'s school') if not grade: grade = self.get_gerade() self.name = name self.school = school self.grade = grade self.print_student() def get_gerade(self): while True: grade = raw_input('What is the student \'s [k, 1-5]') if grade.lower() not in ['k', '1', '2', '3', '4', '5']: print 'I \'m sorry, but {} isn\'t valid.'.format(self.grade) else: return grade def print_student(self): print 'Name:{}'.format(self.name) print 'School:{}'.format(self.school) print 'Grade:{}'.format(self.grade) def main(): student1 = Student() student2 = Student(name='Byron Bale', grade='2', school='Minnieville') if __name__ == '__main__': main()
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...