初学者一枚%% 2022-11-21 09:42 采纳率: 86.4%
浏览 9
已结题

写python的时候遇到了这样的一个报错:

问题遇到的现象和发生背景

写python的时候遇到了这样的一个报错:
init() missing 2 required positional arguments: 'name' and 'age'

用代
class Student:
    count = 0
    def printnum(self):
        print('count={}'.format(self.count))
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def printstu(self):
        print('name={},age={}'.format(self.name,self.age))
if __name__=='__main__':
    s1 = Student()
    s2 = Student('Tom','18')
    print('s1.count={}'.format(s1.count))
    s1.printnum()  
    s1.count='hello'
    print('Student.count={}'.format(s1.count))
    s2.printstu()
    print('name={} age={}'.format(Student.name,Student.age))



```码块功能插入代码,请勿粘贴截图 

###### 运行结果及报错内容 
预期输出: s1.count=0
                count=0
                Student.count=hello
                s2.name=Tom,s2.age=18
                name=Tom age=18
实际输出: 

```python

Traceback (most recent call last):
  File "src/step1/pratise_1.py", line 12, in <module>
    s1 = Student()
TypeError: __init__() missing 2 required positional arguments: 'name' and 'age'
我的解答思路和尝试过的方法

百度搜过,但是没有结果

我想要达到的结果

帮我解答一下这个报错的出现原因

  • 写回答

4条回答 默认 最新

  • Java大魔王 2022-11-21 09:48
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 创建了问题 11月21日