python programming作业5
python programming作业5
# -*- coding: utf-8 -*-
class ageError(Exception):
pass
class salaryError(Exception):
pass
class staff:
def __init__(self,ID,name,age,salary):
self.ID = ID
self.name = name
self.age = age
self.salary = salary
def change(self,num,value):
if num == 0:
self.ID = value
elif num == 1:
self.name = value
elif num == 2:
self.age = value
elif num == 3:
self.salary = value
def __str__(self):
describe = "ID为"+str(self.ID)+"的职员"+str(self.name)+",年龄为"+str(self.age)+",薪水每月"+str(self.salary)+"$"
return describe
def check(self):
is_ok = True
try:
self.ID = int(self.ID)
except ValueError:
is_ok = False
print("错误!输入的ID不是整数!")
try:
self.age = int(self.age)
if self.age <0 or self.age >100:
raise ageError
except ValueError:
is_ok = False
print("错误!输入的年龄不是整数!")
except ageError:
is_ok = False
print("错误!输入的年龄不合法!")
try:
self.salary = float(self.salary)
if self.salary <0:
raise salaryError
except ValueError:
is_ok = False
print("错误!输入的薪水不是一个数!")
except salaryError:
is_ok = False
print("错误!输入的薪水不合法!")
return is_ok
cys = staff(1,'蔡军帅',18,0)
cys.change(3,88888888)
if cys.check():
print(cys.__str__())
allStaff=[]
allStaff.append(cys)
repeat = False
while True:
if repeat:
ID,name,age,salary = input("请重新输入职员信息,ID为0表示录入结束\n").split()
else:
ID,name,age,salary = input("请输入职员信息,ID为0表示录入结束\n").split()
if ID == '0':
print("录入结束")
break
new_person = staff(ID,name,age,salary)
if new_person.check():
print("正确,正在录入信息......")
print("录入成功!")
print(new_person.__str__())
allStaff.append(new_person)
repeat = False
else:
repeat = True
print("所有职工信息如下:")
for person in allStaff:
print(person.__str__())