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

写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日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度