编写一个学生类,包含的属性有学号、姓名、性别、年龄,将所有学生存储在一个数组中,编写学生管理程序实现如下操作:1、增加一个学生2、根据学号删除某个学生3、将所有学生年龄增加一倍4、按数组中顺序显示所有学生
时间: 2023-05-29 15:03:06 浏览: 76
class Student:
def __init__(self, num, name, gender, age):
self.num = num
self.name = name
self.gender = gender
self.age = age
def __str__(self):
return '学号:{}\t姓名:{}\t性别:{}\t年龄:{}'.format(self.num, self.name, self.gender, self.age)
def double_age(self):
self.age *= 2
students = []
def add_student():
num = input('请输入学号:')
name = input('请输入姓名:')
gender = input('请输入性别:')
age = int(input('请输入年龄:'))
student = Student(num, name, gender, age)
students.append(student)
print('添加成功!')
def delete_student():
num = input('请输入要删除的学生学号:')
for student in students:
if student.num == num:
students.remove(student)
print('删除成功!')
return
print('找不到该学生!')
def double_age():
for student in students:
student.double_age()
print('所有学生年龄已经增加一倍!')
def show_students():
for student in students:
print(student)
while True:
print('1、增加一个学生')
print('2、根据学号删除某个学生')
print('3、将所有学生年龄增加一倍')
print('4、按数组中顺序显示所有学生')
print('5、退出程序')
choice = input('请输入操作编号:')
if choice == '1':
add_student()
elif choice == '2':
delete_student()
elif choice == '3':
double_age()
elif choice == '4':
show_students()
elif choice == '5':
break
else:
print('无效的操作编号!')
阅读全文