-3

OK、Python で最初のプロジェクトの RPG を作成していますが、問題があります。コードは次のとおりです。

def getName():
    tempName = ""
    while 1:
        tempName = nameInput("What is you name?")

        if len(tempName) < 1:
            continue

            yes = yesOrNo( tempName + ", is that your name?")

            if yes:
                return tempName
            else:
                continue

これが主な定義です:

    player.name = getName

while (not player.dead):
    line = raw_input(">>")
    input = line.split()
    input.append("EOI")

    if isValidCMD(input[0]):
        runCMD(input[0], input[1], player)

ここに問題があります。main(player) を実行すると、「あなたの名前は何ですか?」ではなく、開始時に >> プロンプトが表示されるようです。ストリング。

ここで取引は何ですか?ああ、これはpython 2.7です

編集: わかりました () を getName 関数に追加しましたが、名前の確認に進まないまま実行され続けます

4

2 に答える 2

4

関数を呼び出す必要があります。

player.name = getName()

Python では、関数は値です。コードでは、プレーヤー名を関数に設定していますが、実際には実行していません。を追加する()と、関数が実行さplayer.nameれ、その結果が設定されます。

固定コードは次のとおりです。

def getName():
    tempName = ""
    while True:
        tempName = raw_input("What is you name? ")
        if not len(tempName):
            continue
        if yesOrNo("Is {0} your name?".format(tempName)):
            return tempName
        else:
            continue

そして主な機能:

player.name = getName()

while not player.dead:
    input = raw_input(">> ").split()
    input.append("EOI")

    if isValidCMD(input[0]):
        runCMD(input[0], input[1], player)
于 2013-08-15T00:48:45.797 に答える
0
if len(tempName) < 1:
    continue
    # oh no, you never ended the if statement
    # the rest of the code is still inside the if
    # so it never runs, because you already continued
    # to fix this, unindent the rest of the code in the method
    yes = yesOrNo( tempName + ", is that your name?")

    if yes:
        return tempName
    else:
        continue

メソッドの残りの部分はifcontinue. インデントが重要であることを忘れないでください。

また、関数を呼び出すこともないgetNameため、当然、その中のコードは決して実行されません。

player.name = getName # this is not calling the function!
player.name = getName() # you need parentheses
于 2013-08-15T00:50:13.810 に答える