简体   繁体   English

Python错误:TypeError:…缺少1个必需的位置参数:

[英]Python ERROR: TypeError: … missing 1 required positional argument:

class Estudante:
    def __init__(self,nome,numero_estudante):
        self.nome=nome
        self.numero_estudante=numero_estudante
        self.cadeiras = []

    def enrol (self, curso_decorrer):
        self.cadeiras.append(curso_decorrer)
        curso_decorrer.add_estudante(self)

class Departamento:
    def __init__(self,nome, codigo_departamento, local):
        self.nome=nome
        self.codigo_departamento = codigo_departamento
        self.local= local
        self.cursos = []

    def add_curso(self,descricao,codigo_curso, creditos,departamento):
        self.cursos[codigo_curso] = Curso(self,descricao,codigo_curso, creditos,departamento)
        return self.cursos[codigo_curso]

class Curso:
    def __init__(self,descricao,codigo_curso, creditos,departamento):
        self.descricao = descricao
        self.codigo_curso= codigo_curso
        self.creditos=creditos
        self.departamento=departamento
        self.departamento.add_curso(self)
        self.decorridos =[]

    def adicionar_ano(self,ano):
        self.decorridos.append(Cursodecorrer(self,ano))
        return self.decorridos[-1]

class Cursodecorrer:
    def __init__(self,curso,ano):
        self.curso = curso
        self.ano = ano
        self.estudantes =[]

    def adicionar_estudante(self,estudante):
        self.estudantes.append(estudante)

Engenharia=Departamento("Departamento de Engenharia","001","Azurém")
Matemática=Departamento("Departamento de Matemática","002","Braga")
MIEBIOM=Departamento.add_curso("Engenharia Biomédica",'001-1',"55","Engenharia")
MIEBIOL=Departamento.add_curso("Engenharia Biológica",'001-2',"55","Engenharia")
MAT=Departamento.add_curso("Matemática",'002-1',"30")
MIEBIOM_2017=Curso.adicionar_ano("2017")
A74000=Estudante("Pedro Miguel","74000")

Code error: MIEBIOM=Departamento.add_curso("Engenharia Biomédica",'001-1',"55","Engenharia")** 代码错误:MIEBIOM = Departamento.add_curso(“ EngenhariaBiomédica”,'001-1',“ 55”,“ Engenharia”)**

TypeError: add_curso() missing 1 required positional argument: 'departamento'

I tried everything, anyone knows what's wrong? 我尝试了一切,有人知道出什么事了吗?

Calling a method directly on a class you should only do with class methods. 直接在类上调用方法只应使用类方法。 You should first create an instance of Departamento on which you can call the instance method add_curso. 您首先应该创建一个Departamento实例,您可以在该实例上调用实例方法add_curso。

Here's an example of such a code where you first create a Departamento and then add a curso to it, which I guess is what you want to do: 这是这样一个代码的示例,在该代码中,您首先创建一个Departamento,然后向其中添加一个curso,我想这就是您想要做的:

dep = Departamento("name", "codigo dep", "local")
dep.add_curso("Engenharia Biomédica",'001-1',"55","Engenharia")

You can see it is an instance method because the first argument in the method definition is self (this is a convention). 您可以看到它是一个实例方法,因为方法定义中的第一个参数是self(这是一个约定)。

More about class methods vs instance methods: Difference between Class and Instance methods 有关类方法与实例方法的更多信息: 类和实例方法之间的区别

Your add_curso function requires 5 values 您的add_curso函数需要5个值

def add_curso(self,descricao,codigo_curso, creditos,departamento):

But you are passing only 4 values when you are calling it in this line: 但是,在此行中调用它时,您仅传递4个值:

MIEBIOM=Departamento.add_curso("Engenharia Biomédica",'001-1',"55","Engenharia")

So Python interprets it like this: 因此,Python解释如下:

self = "Engenharia Biomedica"
descricao = "001-1"
codigo_curso = "55"
creditos = "Engenharia"
departamento = !!Missing Value!!

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM