0

現在、私のコードに少し問題があります。私は非常に基本的な RPG を作成していますが、この問題に遭遇しました: (unbound method wrongCommand.wrong) Python 2.7.5 と Windows 7 も実行しています。

これが私のコードです:

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()
4

1 に答える 1

2

まず、変更したいのは

print wrongCommand.wrong

print wrongCommand.wrong()

(注: 開き括弧と閉じ括弧の追加)

ただし、wrongメソッドから出力された行、そのメソッドの戻り値 (現在は None) を取得します。

だったら俺はきっと変わるだろう

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() は正確に 1 つの引数を必要としますが、引数がありません」のようなエラーが発生します。

または、間違ったメソッドをクラス メソッドまたは静的メソッドとして定義することもできます。

@staticmethod
def wrong():
    # ...

また

@classmethod
def wrong(cls):
    #...
于 2013-09-26T00:22:22.800 に答える