导致过早退出程序的变量值

2024-07-03 06:34:04 发布

您现在位置:Python中文网/ 问答频道 /正文

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

最后一个功能是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.')

Tags: oftheto程序功能you游戏print
3条回答

通过查看您的代码,我认为您使用的是python3。如果您使用的是python2,那么使用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

在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.')

这里的输入函数被误用了。当使用“input”时,Python解释器尝试评估输入的内容。这就是为什么使用发回字符串的原始输入更安全的原因。您可以使用以下内容调整代码的开头:

def gameEnd():
    playAgain = raw_input('Play again ? (Y/y)')
    if any([playInput == 'Y', playInput == 'y']):
       gameCore()

相关问题 更多 >