Python(未绑定方法errocommand.error)

Python(未绑定方法errocommand.error),python,Python,我目前的代码有点问题。我正在制作一个非常基本的RPG,遇到了这个问题: (未绑定方法错误命令。错误) 我还运行Python2.7.5和Windows7 这是我的密码: import os class wrongCommand(): def wrong(): os.system("cls") print "Sorry, the command that you entered is invalid." print "Please try ag

我目前的代码有点问题。我正在制作一个非常基本的RPG,遇到了这个问题: (未绑定方法错误命令。错误) 我还运行Python2.7.5和Windows7

这是我的密码:

import os
class wrongCommand():
    def wrong():
        os.system("cls")
        print "Sorry, the command that you entered is invalid."
        print "Please try again."


def main():
    print "Welcome to the game!"
    print "What do you want to do?"
    print "1.) Start game"
    print "2.) More information/Credits"
    print "3.) Exit the game"
    mm = raw_input("> ")
    if mm != "1" and mm != "2" and mm != "3":
        print wrongCommand.wrong
        main();

main()

所以首先,你要改变

print wrongCommand.wrong

(注:添加打开和关闭参数)

但是,您将得到从
错误的
方法打印的行以及该方法的返回值,该值当前为空

所以我可能会改变

print wrongCommand.wrong()
简单地

wrongCommand.wrong()
(注意:删除
打印
语句)

或者,您可以让
error()
返回一个字符串,而不是打印一个字符串,然后返回此行

print wrongCommand.wrong()
好的


您必须调用类实例的
error()
方法,例如

或者干脆

wrongCommand().wrong()
在这两种情况下,您都必须将
error()
方法定义更改为

def wrong(self):
    #...
或者,您将得到一个错误,如“error()只需要一个参数,get none”

或者,您可以将错误的方法定义为类方法或静态方法:

@staticmethod
def wrong():
    # ...


奇怪的当我尝试你建议的方法时,我得到了以下结果:TypeError:unbound method Error()必须以错误的命令实例作为第一个参数来调用(没有任何结果)是的,你要么在类实例上调用它,要么使该方法成为静态或类方法——一秒钟,我将更新,我就快到了!我已经尝试了你建议的前两种方法。还有一个错误!>\uu>ErrorCommand().Error()类型错误:Error()不接受任何参数(给定1个),我收回这一点。我想出来了。。。我只是在类中错误地将init放在def中。它起作用了。也像自己一样尝试了这个论点,它成功了。所以我不知道我把它放在哪里或者什么都不重要。但它是有效的PIs你把
错误的
放入一个类中有什么原因吗?让它成为一个独立的函数,在文件的顶层定义会更有意义(尽管您可能需要给它一个更长、更具描述性的名称)。使用
类foo(对象):
def wrong(self):
    #...
@staticmethod
def wrong():
    # ...
@classmethod
def wrong(cls):
    #...