Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
TypeError: Mean() missing 1 required positional argument: 'data'

I'm trying to program a basic mean calculator using classes. However, I'm getting the error

TypeError: Mean() missing 1 required positional argument: 'data'

I have two files: one which contains the class with the mean function and then one which calls it, and that is when I'm getting the error. My code is:

class Statistics:
    def __init__(self,mean_x,mean_y,var,covar):
        self.mean_x=mean_x
        self.mean_y=mean_y
        self.var=var
        self.covar=covar
    
    def Mean(self,data):
        return sum(data)/float(len(data))

And the code which throws the error is:

from Statistics import Statistics 
X=(0,1,3,5)
mean_x=Statistics.Mean(X)
print(mean_x)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Mean is an instance method, so you need to call it on an instance (which will become the self argument for the method invocation).

statistics = Statistics(None, None, None, None)
mean_x = statistics.Mean((0, 1, 3, 5))

Since the parameters on Statistics.__init__ aren't used I'd suggest removing them (or just removing the __init__ altogether):

class Statistics:
   
    def mean(self, data):
        return sum(data)/float(len(data))
from Statistics import Statistics 
X = (0,1,3,5)
statistics = Statistics()
mean_x = statistics.mean(X)
print(mean_x)

Note that Python comes with a statistics module that has a mean function built in:

import statistics

X = (0,1,3,5)
mean_x = statistics.mean(X)
print(mean_x)
over 4 years ago · Santiago Trujillo Relatório

0

You define the instance method, but there is no instance.

Also, it is better to change the 'Mean' to 'mean'.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda