鱼C论坛

 找回密码
 立即注册
查看: 1520|回复: 6

[已解决]求助,python类函数的使用

[复制链接]
发表于 2021-8-7 15:09:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
函数如下
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[i] = input().split()
    num_1 = int(a[i][0])
    xueye_1 = int(a[i][1])
    sutuo_1 = int(a[i][2])
    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
最佳答案
2021-8-7 15:46:27
我查了一下,如果修饰的话调用的时候不加括号

  1. n=int(input())
  2. a = []
  3. b = []
  4. for i in range(n):
  5.     a.append(f'student_{i}')

  6. class student:
  7.     def __init__(self,num,xueye,sutuo):
  8.        self.num = num
  9.        self.xueye = xueye
  10.        self.sutuo = sutuo
  11.     @property
  12.     def pingjia(self):
  13.         biaoxian = self.xueye*0.7+self.sutuo*0.3
  14.         zongfen = self.xueye+self.sutuo
  15.         if biaoxian>=80 and zongfen>140:
  16.             return 'Excellent'
  17.         else:
  18.             return 'Not excellent'
  19. for i in range(n):
  20.     a[i] = input().split()
  21.     num_1 = int(a[i][0])
  22.     xueye_1 = int(a[i][1])
  23.     sutuo_1 = int(a[i][2])
  24.     b.append(student(num_1,xueye_1,sutuo_1))
  25. for i in b:
  26.     print(i.pingjia)

复制代码


https://zhuanlan.zhihu.com/p/64487092
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-7 15:20:27 | 显示全部楼层
__init__双下划线
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:28:05 | 显示全部楼层
逃兵 发表于 2021-8-7 15:20
__init__双下划线

呀,没注意,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:28:40 | 显示全部楼层
撕裂天堂 发表于 2021-8-7 15:28
呀,没注意,感谢

但是我改完了还是有错误,报错
print(i.pingjia())

TypeError: 'str' object is not callable

这个是哪里错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-7 15:39:42 | 显示全部楼层
撕裂天堂 发表于 2021-8-7 15:28
但是我改完了还是有错误,报错
print(i.pingjia())

修饰符引起的报错,具体原因不知道,注释掉就行了
  1. n=int(input())
  2. a = []
  3. b = []
  4. for i in range(n):
  5.     a.append(f'student_{i}')

  6. class student:
  7.     def __init__(self,num,xueye,sutuo):
  8.        self.num = num
  9.        self.xueye = xueye
  10.        self.sutuo = sutuo
  11.     #@property
  12.     def pingjia(self):
  13.         biaoxian = self.xueye*0.7+self.sutuo*0.3
  14.         zongfen = self.xueye+self.sutuo
  15.         if biaoxian>=80 and zongfen>140:
  16.             return 'Excellent'
  17.         else:
  18.             return 'Not excellent'
  19. for i in range(n):
  20.     a[i] = input().split()
  21.     num_1 = int(a[i][0])
  22.     xueye_1 = int(a[i][1])
  23.     sutuo_1 = int(a[i][2])
  24.     b.append(student(num_1,xueye_1,sutuo_1))
  25. for i in b:
  26.     print(i.pingjia())

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-7 15:46:27 | 显示全部楼层    本楼为最佳答案   
我查了一下,如果修饰的话调用的时候不加括号

  1. n=int(input())
  2. a = []
  3. b = []
  4. for i in range(n):
  5.     a.append(f'student_{i}')

  6. class student:
  7.     def __init__(self,num,xueye,sutuo):
  8.        self.num = num
  9.        self.xueye = xueye
  10.        self.sutuo = sutuo
  11.     @property
  12.     def pingjia(self):
  13.         biaoxian = self.xueye*0.7+self.sutuo*0.3
  14.         zongfen = self.xueye+self.sutuo
  15.         if biaoxian>=80 and zongfen>140:
  16.             return 'Excellent'
  17.         else:
  18.             return 'Not excellent'
  19. for i in range(n):
  20.     a[i] = input().split()
  21.     num_1 = int(a[i][0])
  22.     xueye_1 = int(a[i][1])
  23.     sutuo_1 = int(a[i][2])
  24.     b.append(student(num_1,xueye_1,sutuo_1))
  25. for i in b:
  26.     print(i.pingjia)

复制代码


https://zhuanlan.zhihu.com/p/64487092
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-8-7 15:57:16 | 显示全部楼层
逃兵 发表于 2021-8-7 15:46
我查了一下,如果修饰的话调用的时候不加括号

感谢感谢,整明白了!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 14:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表