0

我正在开发一个简单的游戏程序,但在游戏的初始启动时遇到了问题。我设置了三个功能:开始,游戏,结束。问题是,一旦程序创建了函数,它就会终止,因为没有重新启动来再次通过函数。

这是最后一个函数,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()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

这里的输入函数被误用了。当使用“输入”时,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 回答