class Student: def __init__(self, sid,name,height,sex): self.sid=sid self.name=name self.height=height self.sex=sex def print(): 补充代码,打印本同学的全部信息
时间: 2023-06-15 09:07:01 浏览: 139
你需要在print()方法中添加self参数来引用对象的属性。
代码如下所示:
```
class Student:
def __init__(self, sid, name, height, sex):
self.sid = sid
self.name = name
self.height = height
self.sex = sex
def print(self):
print("学号:", self.sid)
print("姓名:", self.name)
print("身高:", self.height)
print("性别:", self.sex)
```
然后你可以创建一个学生对象,并使用print()方法打印学生的全部信息:
```
s = Student("001", "张三", 175, "男")
s.print()
```
输出结果为:
```
学号: 001
姓名: 张三
身高: 175
性别: 男
```
相关问题
class Student(Person): def __init__(self, college, name, age, sex): super().__init__(name, age, sex) self.college=college self.class=class def personInfo(self): super().personInfo() print("学院:",self.college,
class Student(Person):
def __init__(self, college, name, age, sex, class_name):
super().__init__(name, age, sex)
self.college = college
self.class_name = class_name
def studentInfo(self):
print("姓名:", self.name, "年龄:", self.age, "性别:", self.sex, "学院:", self.college, "班级:", self.class_name)
def personInfo(self):
super().personInfo()
print("学院:", self.college, "班级:", self.class_name)
# 示例调用
s1 = Student("清华大学", "张三", 20, "男", "计算机科学与技术1班")
s1.personInfo()
s1.studentInfo()
python代码:定义一个学生类和学生类对象的顺序表类,完成以下函数的补充。 2.1 补充学生类的定义 class Student: def __init__(self, sid,name,height,sex): self.sid=sid self.name=name self.height=height self.sex=sex def print(): #补充代码,打印本同学的全部信息 2.2 补充学生顺序表的定义 class SequenceTSet(): def __init__(self): self.TSet=[ ] def readFile(self, filname): #补充代码,能将文件中的信息,读入到self.Tset中 def writeFile(self, filname): #补充代码,能将self.Tset信息,写入到文件filname中 def sortAndprint(self,filename): #补充代码,对TSet中的学生的身高进行升序排序 def cntHeight(self, y): #补充代码,输出TSet中身高大于y的学生个数 def splitSet(self, x,filename): #补充代码,能将TSet中性别为x的学生都删除 def del(self, sid): #补充代码,删除TSet中学号为sid的学生 def insert(self, hei): #补充代码,添加身高为hei的学生到TSet中,要求添加学生后,学生表中的身高保持有升序。 建立如下的 学生信息文件 ,每行存入 学号、姓名、 性别、 身高等信息 ,存为student.txt文件 ,读此文件 ,调用 学生类中的所有函数,进行数据 维护后,重新存回到文件 student.txt中 。 1 杨勇 0 1.78 2 左千 1 1.64
2.1 补充学生类的定义
```python
class Student:
def __init__(self, sid,name,height,sex):
self.sid=sid
self.name=name
self.height=height
self.sex=sex
def print(self):
print("学号:", self.sid)
print("姓名:", self.name)
print("身高:", self.height)
if self.sex == 0:
print("性别:男")
else:
print("性别:女")
```
2.2 补充学生顺序表的定义
```python
class SequenceTSet():
def __init__(self):
self.TSet=[]
def readFile(self, filname):
with open(filname, 'r') as f:
lines = f.readlines()
for line in lines:
sid, name, sex, height = line.strip().split()
sex = int(sex)
height = float(height)
student = Student(sid, name, height, sex)
self.TSet.append(student)
def writeFile(self, filname):
with open(filname, 'w') as f:
for student in self.TSet:
f.write(student.sid + ' ' + student.name + ' ' + str(student.sex) + ' ' + str(student.height) + '\n')
def sortAndprint(self,filename):
self.TSet.sort(key=lambda x: x.height)
self.writeFile(filename)
for student in self.TSet:
student.print()
def cntHeight(self, y):
cnt = 0
for student in self.TSet:
if student.height > y:
cnt += 1
print("身高大于", y, "的学生个数为:", cnt)
def splitSet(self, x,filename):
self.TSet = [student for student in self.TSet if student.sex != x]
self.writeFile(filename)
def del_student(self, sid):
for student in self.TSet:
if student.sid == sid:
self.TSet.remove(student)
return
print("学号为", sid, "的学生不存在")
def insert(self, hei):
student = Student(str(len(self.TSet) + 1), "", hei, 0)
self.TSet.append(student)
self.TSet.sort(key=lambda x: x.height)
```
读取学生信息文件 student.txt ,调用函数进行数据维护后,重新存回到文件 student.txt 中。
```python
sequencetset = SequenceTSet()
sequencetset.readFile('student.txt')
sequencetset.sortAndprint('student.txt')
sequencetset.cntHeight(1.70)
sequencetset.splitSet(0, 'student.txt')
sequencetset.del_student('1')
sequencetset.insert(1.75)
sequencetset.writeFile('student.txt')
```
阅读全文