1•设计一个父类Person类,包含数据成员:身份证号、姓名、性别、出生日期,要求可以完成以下操作: 1)可以从键盘输入基本信息; 2) 可以输出基本信息到屏幕; 3)可以修改基本信息; 4)可以比较两个人的年纪大小; 5)可以计算出两个人出生日期相差的天数。 再设计一个子类student类,在父类的基础上新增数据成员:学号、语文、数学、英语成绩,要求可以完成以下操作: 1)可以从键盘输入学生信息; 2) 可以输出学生信息到屏幕; 3) 可以修改学生基本信息; 4) 可以计算每位学生的语数英平均分; 5)可以按多个学生的平均分从高到低排序,并输出到屏幕; 6)可以比较两个学生年纪大小; 7)可以计算出两个学生出生日期相差的天数。
时间: 2023-05-21 08:03:12 浏览: 309
好的,我可以回答这个问题。以下是Person类和Student类的完整代码:
```python
import datetime
class Person:
def __init__(self, id, name, gender, birthdate):
self.id = id
self.name = name
self.gender = gender
self.birthdate = birthdate
def input_info(self):
self.id = input("请输入身份证号:")
self.name = input("请输入姓名:")
self.gender = input("请输入性别:")
birthdate_str = input("请输入出生日期(格式为YYYY-MM-DD):")
self.birthdate = datetime.datetime.strptime(birthdate_str, "%Y-%m-%d").date()
def output_info(self):
print("身份证号:", self.id)
print("姓名:", self.name)
print("性别:", self.gender)
print("出生日期:", self.birthdate.strftime("%Y-%m-%d"))
def modify_info(self):
choice = input("请选择要修改的信息(1.身份证号 2.姓名 3.性别 4.出生日期):")
if choice == "1":
self.id = input("请输入新的身份证号:")
elif choice == "2":
self.name = input("请输入新的姓名:")
elif choice == "3":
self.gender = input("请输入新的性别:")
elif choice == "4":
birthdate_str = input("请输入新的出生日期(格式为YYYY-MM-DD):")
self.birthdate = datetime.datetime.strptime(birthdate_str, "%Y-%m-%d").date()
else:
print("输入有误,请重新选择!")
def compare_age(self, other):
today = datetime.date.today()
age1 = today.year - self.birthdate.year - ((today.month, today.day) < (self.birthdate.month, self.birthdate.day))
age2 = today.year - other.birthdate.year - ((today.month, today.day) < (other.birthdate.month, other.birthdate.day))
if age1 > age2:
print(self.name, "比", other.name, "年龄大!")
elif age1 < age2:
print(self.name, "比", other.name, "年龄小!")
else:
print(self.name, "和", other.name, "年龄相同!")
def days_diff(self, other):
diff = abs((self.birthdate - other.birthdate).days)
print(self.name, "和", other.name, "出生日期相差", diff, "天。")
class Student(Person):
def __init__(self, id, name, gender, birthdate, student_id, chinese_score, math_score, english_score):
super().__init__(id, name, gender, birthdate)
self.student_id = student_id
self.chinese_score = chinese_score
self.math_score = math_score
self.english_score = english_score
def input_info(self):
super().input_info()
self.student_id = input("请输入学号:")
self.chinese_score = float(input("请输入语文成绩:"))
self.math_score = float(input("请输入数学成绩:"))
self.english_score = float(input("请输入英语成绩:"))
def output_info(self):
super().output_info()
print("学号:", self.student_id)
print("语文成绩:", self.chinese_score)
print("数学成绩:", self.math_score)
print("英语成绩:", self.english_score)
def modify_info(self):
super().modify_info()
choice = input("请选择要修改的信息(1.学号 2.语文成绩 3.数学成绩 4.英语成绩):")
if choice == "1":
self.student_id = input("请输入新的学号:")
elif choice == "2":
self.chinese_score = float(input("请输入新的语文成绩:"))
elif choice == "3":
self.math_score = float(input("请输入新的数学成绩:"))
elif choice == "4":
self.english_score = float(input("请输入新的英语成绩:"))
else:
print("输入有误,请重新选择!")
def avg_score(self):
avg = (self.chinese_score + self.math_score + self.english_score) / 3
print(self.name, "的语数英平均分为:", avg)
def compare_age(self, other):
super().compare_age(other)
def days_diff(self, other):
super().days_diff(other)
students = []
n = int(input("请输入学生人数:"))
for i in range(n):
print("请输入第", i+1, "个学生的信息:")
student = Student("", "", "", "", "", 0, 0, 0)
student.input_info()
students.append(student)
print("所有学生的信息如下:")
for student in students:
student.output_info()
while True:
choice = input("请选择要进行的操作(1.修改学生信息 2.计算每位学生的语数英平均分 3.按多个学生的平均分从高到低排序并输出到屏幕 4.比较两个学生年龄大小 5.计算两个学生出生日期相差的天数 6.退出程序):")
if choice == "1":
student_id = input("请输入要修改信息的学生的学号:")
for student in students:
if student.student_id == student_id:
student.modify_info()
break
else:
print("未找到该学生!")
elif choice == "2":
for student in students:
student.avg_score()
elif choice == "3":
sorted_students = sorted(students, key=lambda x: (x.chinese_score + x.math_score + x.english_score) / 3, reverse=True)
print("按平均分从高到低排序的学生信息如下:")
for student in sorted_students:
student.output_info()
elif choice == "4":
student_id1 = input("请输入第一个学生的学号:")
student_id2 = input("请输入第二个学生的学号:")
student1 = None
student2 = None
for student in students:
if student.student_id == student_id1:
student1 = student
elif student.student_id == student_id2:
student2 = student
if student1 and student2:
break
if student1 and student2:
student1.compare_age(student2)
else:
print("未找到该学生!")
elif choice == "5":
student_id1 = input("请输入第一个学生的学号:")
student_id2 = input("请输入第二个学生的学号:")
student1 = None
student2 = None
for student in students:
if student.student_id == student_id1:
student1 = student
elif student.student_id == student_id2:
student2 = student
if student1 and student2:
break
if student1 and student2:
student1.days_diff(student2)
else:
print("未找到该学生!")
elif choice == "6":
break
else:
print("输入有误,请重新选择!")
```
希望这个代码可以帮到你!
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)