相关疑难解决方法(0)

Python解释器错误,x不带参数(给定1个)

我正在写一小段python作为家庭作业,我不会让它运行!我没有那么多的Python经验,但我知道很多Java.我正在尝试实现粒子群优化算法,这就是我所拥有的:

class Particle:    

    def __init__(self,domain,ID):
        self.ID = ID
        self.gbest = None
        self.velocity = []
        self.current = []
        self.pbest = []
        for x in range(len(domain)):
            self.current.append(random.randint(domain[x][0],domain[x][1])) 
            self.velocity.append(random.randint(domain[x][0],domain[x][1])) 
            self.pbestx = self.current          

    def updateVelocity():
    for x in range(0,len(self.velocity)):
        self.velocity[x] = 2*random.random()*(self.pbestx[x]-self.current[x]) + 2 * random.random()*(self.gbest[x]-self.current[x]) 


    def updatePosition():    
        for x in range(0,len(self.current)):
            self.current[x] = self.current[x] + self.velocity[x]    

    def updatePbest():
        if costf(self.current) < costf(self.best):
            self.best = self.current        

    def psoOptimize(domain,costf,noOfParticles=20, noOfRuns=30):
        particles = []
        for i in range(noOfParticles):    
            particle = Particle(domain,i)    
            particles.append(particle)    

        for i in range(noOfRuns): …
Run Code Online (Sandbox Code Playgroud)

python methods arguments object

60
推荐指数
3
解决办法
9万
查看次数

"<method>不带参数(给出1个)"但我没有给出任何参数

我是Python的新手,我编写了这个简单的脚本:

#!/usr/bin/python3
import sys

class Hello:
    def printHello():
        print('Hello!')

def main():
    helloObject = Hello()
    helloObject.printHello()   # Here is the error

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

当我运行它(./hello.py)时,我收到以下错误消息:

Traceback (most recent call last):
  File "./hello.py", line 13, in <module>
    main()
  File "./hello.py", line 10, in main
    helloObject.printHello()
TypeError: printHello() takes no arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

为什么Python认为我给出了printHello()一个参数,而我显然没有?我做错了什么?

python methods call

24
推荐指数
3
解决办法
2万
查看次数

标签 统计

methods ×2

python ×2

arguments ×1

call ×1

object ×1