python programming作业5
# -*- coding: utf-8 -*-
class ageError(Exception):pass
class salaryError(Exception):passclass staff:def __init__(self,ID,name,age,salary):self.ID &#61; IDself.name &#61; nameself.age &#61; ageself.salary &#61; salarydef change(self,num,value):if num &#61;&#61; 0:self.ID &#61; valueelif num &#61;&#61; 1:self.name &#61; valueelif num &#61;&#61; 2:self.age &#61; valueelif num &#61;&#61; 3:self.salary &#61; valuedef __str__(self):describe &#61; "ID为"&#43;str(self.ID)&#43;"的职员"&#43;str(self.name)&#43;",年龄为"&#43;str(self.age)&#43;",薪水每月"&#43;str(self.salary)&#43;"$"return describedef check(self):is_ok &#61; Truetry:self.ID &#61; int(self.ID)except ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的ID不是整数&#xff01;")try:self.age &#61; int(self.age)if self.age <0 or self.age >100:raise ageErrorexcept ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的年龄不是整数&#xff01;") except ageError:is_ok &#61; Falseprint("错误&#xff01;输入的年龄不合法!")try:self.salary &#61; float(self.salary) if self.salary <0:raise salaryErrorexcept ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的薪水不是一个数&#xff01;") except salaryError:is_ok &#61; Falseprint("错误&#xff01;输入的薪水不合法!")return is_okcys &#61; staff(1,&#39;蔡军帅&#39;,18,0)
cys.change(3,88888888)
if cys.check():print(cys.__str__())allStaff&#61;[]
allStaff.append(cys)
repeat &#61; False
while True:if repeat:ID,name,age,salary &#61; input("请重新输入职员信息,ID为0表示录入结束\n").split()else:ID,name,age,salary &#61; input("请输入职员信息,ID为0表示录入结束\n").split()if ID &#61;&#61; &#39;0&#39;:print("录入结束")breaknew_person &#61; staff(ID,name,age,salary)if new_person.check():print("正确&#xff0c;正在录入信息......")print("录入成功!")print(new_person.__str__()) allStaff.append(new_person)repeat &#61; Falseelse:repeat &#61; Trueprint("所有职工信息如下&#xff1a;")
for person in allStaff:print(person.__str__())
posted on 2019-03-27 10:04 蔡军帅_ACM 阅读(...) 评论(...) 编辑 收藏
# -*- coding: utf-8 -*-
class ageError(Exception):pass
class salaryError(Exception):passclass staff:def __init__(self,ID,name,age,salary):self.ID &#61; IDself.name &#61; nameself.age &#61; ageself.salary &#61; salarydef change(self,num,value):if num &#61;&#61; 0:self.ID &#61; valueelif num &#61;&#61; 1:self.name &#61; valueelif num &#61;&#61; 2:self.age &#61; valueelif num &#61;&#61; 3:self.salary &#61; valuedef __str__(self):describe &#61; "ID为"&#43;str(self.ID)&#43;"的职员"&#43;str(self.name)&#43;",年龄为"&#43;str(self.age)&#43;",薪水每月"&#43;str(self.salary)&#43;"$"return describedef check(self):is_ok &#61; Truetry:self.ID &#61; int(self.ID)except ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的ID不是整数&#xff01;")try:self.age &#61; int(self.age)if self.age <0 or self.age >100:raise ageErrorexcept ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的年龄不是整数&#xff01;") except ageError:is_ok &#61; Falseprint("错误&#xff01;输入的年龄不合法!")try:self.salary &#61; float(self.salary) if self.salary <0:raise salaryErrorexcept ValueError:is_ok &#61; Falseprint("错误&#xff01;输入的薪水不是一个数&#xff01;") except salaryError:is_ok &#61; Falseprint("错误&#xff01;输入的薪水不合法!")return is_okcys &#61; staff(1,&#39;蔡军帅&#39;,18,0)
cys.change(3,88888888)
if cys.check():print(cys.__str__())allStaff&#61;[]
allStaff.append(cys)
repeat &#61; False
while True:if repeat:ID,name,age,salary &#61; input("请重新输入职员信息,ID为0表示录入结束\n").split()else:ID,name,age,salary &#61; input("请输入职员信息,ID为0表示录入结束\n").split()if ID &#61;&#61; &#39;0&#39;:print("录入结束")breaknew_person &#61; staff(ID,name,age,salary)if new_person.check():print("正确&#xff0c;正在录入信息......")print("录入成功!")print(new_person.__str__()) allStaff.append(new_person)repeat &#61; Falseelse:repeat &#61; Trueprint("所有职工信息如下&#xff1a;")
for person in allStaff:print(person.__str__())