求助,python类函数的使用
函数如下n=int(input())
a = []
b = []
for i in range(n):
a.append('student_'+str(i))
class student:
def _init_(self,num,xueye,sutuo):
self.num = num
self.xueye = xueye
self.sutuo = sutuo
@property
def pingjia(self):
biaoxian = self.xueye*0.7+self.sutuo*0.3
zongfen = self.xueye+self.sutuo
if biaoxian>=80 and zongfen>140:
return 'Excellent'
else:
return 'Not excellent'
for i in range(n):
a = input().split()
num_1 = int(a)
xueye_1 = int(a)
sutuo_1 = int(a)
b.append(student(num_1,xueye_1,sutuo_1))
for i in b:
i.pingjia()
简述就是定义一个学生类,有学号,学业成绩,素拓成绩,然后根据成绩评价该学生是否优秀,然后我这样写报错
b.append(student(num_1,xueye_1,sutuo_1))
TypeError: student() takes no arguments __init__双下划线 逃兵 发表于 2021-8-7 15:20
__init__双下划线
呀,没注意,感谢 撕裂天堂 发表于 2021-8-7 15:28
呀,没注意,感谢
但是我改完了还是有错误,报错
print(i.pingjia())
TypeError: 'str' object is not callable
这个是哪里错了 撕裂天堂 发表于 2021-8-7 15:28
但是我改完了还是有错误,报错
print(i.pingjia())
修饰符引起的报错,具体原因不知道,注释掉就行了
n=int(input())
a = []
b = []
for i in range(n):
a.append(f'student_{i}')
class student:
def __init__(self,num,xueye,sutuo):
self.num = num
self.xueye = xueye
self.sutuo = sutuo
#@property
def pingjia(self):
biaoxian = self.xueye*0.7+self.sutuo*0.3
zongfen = self.xueye+self.sutuo
if biaoxian>=80 and zongfen>140:
return 'Excellent'
else:
return 'Not excellent'
for i in range(n):
a = input().split()
num_1 = int(a)
xueye_1 = int(a)
sutuo_1 = int(a)
b.append(student(num_1,xueye_1,sutuo_1))
for i in b:
print(i.pingjia())
我查了一下,如果修饰的话调用的时候不加括号
n=int(input())
a = []
b = []
for i in range(n):
a.append(f'student_{i}')
class student:
def __init__(self,num,xueye,sutuo):
self.num = num
self.xueye = xueye
self.sutuo = sutuo
@property
def pingjia(self):
biaoxian = self.xueye*0.7+self.sutuo*0.3
zongfen = self.xueye+self.sutuo
if biaoxian>=80 and zongfen>140:
return 'Excellent'
else:
return 'Not excellent'
for i in range(n):
a = input().split()
num_1 = int(a)
xueye_1 = int(a)
sutuo_1 = int(a)
b.append(student(num_1,xueye_1,sutuo_1))
for i in b:
print(i.pingjia)
https://zhuanlan.zhihu.com/p/64487092 逃兵 发表于 2021-8-7 15:46
我查了一下,如果修饰的话调用的时候不加括号
感谢感谢,整明白了!!!!!
页:
[1]