2012-12-28 29 views
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.') 
+0

請妥善編輯代碼。爲什麼會有「**」? – ATOzTOA

回答

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

這裏的輸入函數被濫用。當使用'輸入'時,Python解釋器會嘗試評估輸入內容。這就是爲什麼使用發回字符串的raw_input更安全。你可以用下面的適應你的代碼的開頭:

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

通過看你的代碼,我認爲你是如果你使用Python 2然後用raw_input()代替input()使用Python 3。

你的錯誤是當你定義變量時。如果您正在接受用戶的輸入,然後寫入變量,然後將等於符號,然後寫入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