Python(未绑定方法)错误命令。错误)

2024-09-28 23:22:25 发布

您现在位置: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 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()

Tags: andtheto代码yougameosmain
1条回答
网友
1楼 · 发布于 2024-09-28 23:22:25

所以首先,你要改变

print wrongCommand.wrong

print wrongCommand.wrong()

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

但是你会得到从wrong方法打印出来的行,以及该方法的返回值,目前没有。你知道吗

所以我可能会改变

print wrongCommand.wrong()

简单地

wrongCommand.wrong()

(注意:删除print语句)

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

print wrongCommand.wrong()

那就好了。你知道吗


您要么调用类实例wrong()方法,例如

wc = wrongCommand() # Create a new instance
wc.wrong()

或者只是

wrongCommand().wrong()

无论哪种情况,您都必须将wrong()方法定义更改为

def wrong(self):
    #...

否则会出现类似“wrong()只需要一个参数,却没有”这样的错误。你知道吗

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

@staticmethod
def wrong():
    # ...

或者

@classmethod
def wrong(cls):
    #...

相关问题 更多 >