0

私は簡単なゲーム プログラムに取り組んでいますが、ゲームの最初の起動に問題があります。Start、game、end の 3 つの関数を設定しました。問題は、プログラムが関数を作成すると、関数を再度通過するための再起動がないため、プログラムが終了することです。

最後の関数 gameEnd は次のとおりです。

def gameEnd ( ):
       print ('Would you like to play again? Y/N.')
       playAgain = ''     **part of the problem is here; the computer reads playAgain as **
       input (playAgain)    **''. Because playAgain is not equal to Y or y, the program **
       if playAgain == 'Y' or 'y':   **exits without ever starting. I need to move **
           gameCore                  **playAgain somewhere logical.**

       else:
          print ('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
          print ('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')
          if wins > losses:
              print ('Good for you. You have a winning record!')

       elif wins == losses:
              print ('Well, you did okay; neither good nor bad.')

       elif wins < losses:
              print ('Tough luck. You have a losing record.')
              time.sleep (1) 
              print ('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')
4

3 に答える 3

1

コードを見ると、Python 3 を使用していると思います。Python 2 を使用している場合は、raw_input()代わりにinput().

あなたの間違いは、変数を定義していたときでした。ユーザーから入力を取得している場合は、変数を記述し、等号を付けてからraw_input()orを記述しinput()ます。これはあなたがそれを行う方法です:

variable = input() #In python 3
variable = raw_input() #In python 2

だから、このコードを試してください:

def gameEnd():
    print('Would you like to play again? Y/N.')
    playAgain = input("> ")
    if playAgain == 'Y' or playAgain == 'y':
        gameCore()

    else:
        print('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
        print('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')
        if wins > losses:
            print('Good for you. You have a winning record!')

        elif wins == losses:
            print('Well, you did okay; neither good nor bad.')

        elif wins < losses:
            print('Tough luck. You have a losing record.')
            time.sleep(1) 
            print('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')

#End of code
于 2012-12-28T16:20:07.220 に答える
0

gameEnd() でこのバージョンを試してください。ユーザーが「Y」/「y」以外を入力すると終了します。

def gameEnd ( ):
    playAgain = raw_input ('Would you like to play again?')

    if playAgain == 'Y' or playAgain == 'y':
        gameCore()

    else:
        print ('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
        print ('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')

    if wins > losses:
        print ('Good for you. You have a winning record!')
    elif wins == losses:
        print ('Well, you did okay; neither good nor bad.')

    elif wins < losses:
        print ('Tough luck. You have a losing record.')
        time.sleep (1)
        print ('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')
于 2012-12-28T15:46:02.393 に答える
0

ここでの入力関数は誤用されています。'input' を使用すると、Python インタープリターは入力された内容を評価しようとします。そのため、文字列を返す raw_input を使用する方が安全です。コードの先頭を次のように変更できます。

def gameEnd():
    playAgain = raw_input('Play again ? (Y/y)')
    if any([playInput == 'Y', playInput == 'y']):
       gameCore()
于 2012-12-28T16:13:41.543 に答える