2017-06-24 100 views
2

請幫助我完成編程任務的代碼。我試圖運行RollDice()函數,CheckForLaddders()函數& CheckForSnakes()函數在我的main()函數中運行主遊戲。我已經定義了所有的全局變量,如currentPosition1,currentPosition2,ladderList,snakesList & nameList等如何在python中的主函數中運行函數?

當我運行我的代碼時,它說RollDice()沒有被定義 - 這是我在main中調用的初始函數。所以基本上,我如何在代碼的上下文中調用這些函數?我認爲將上述所有4個函數放在一個類(gameOn)中意味着它們可以互相訪問,實質上就變成了方法。但我想我只是沒有正確執行。

我知道我的邏輯是正確的,但我只是一個Python /編碼新手..請幫助!

類gameOn:

def RollDice(): 
    if nameList.index(player) == 0: 
     diceRollValue = randrange(1,7,1) 
     currentPosition1 += diceRollValue 
     print (player, " dice is: ", diceRollValue, ", New position is: ", currentPosition1) 
     if nameList.index(player) == 1: 
      diceRollValue = randrange(1,7,1) 
      currentPosition2 += diceRollValue 
      print (player, " dice is: ", diceRollValue, ", New position is: ", currentPosition2) 

def CheckForLadder(): 
    if nameList.index(player) == 0: 
     if currentPosition1 in ladderList: 
      currentPosition1 += 15 
      print ("Great ", player, " ! It's a ladder, Climb up by 15 cells. Your new position is: ", currentPosition1) 
    if nameList.index(player)== 1: 
     if currentPosition2 in LadderList: 
      currentPosition2 += 15 
      print ("Great ", player, " ! It's a ladder, Climb up by 15 cells. Your new position is: ", currentPosition2) 


def CheckForSnake(): 
    if nameList.index(player)== 0: 
     if currentPosition1 in snakesList: 
      currentPosition1 = currentPosition1 - 10 
      print ("Oops! ", player, " ! You've been bitten, go down 10 cells. Your new position is: ", currentPosition1) 
    if nameList.index(player)== 1: 
     if currentPosition2 in snakesList: 
      currentPosition2 = currentPosition2 - 10 
      print ("Oops! ", player, " ! You've been bitten, go down 10 cells. Your new position is: ", currentPosition2) 


def main(): 
     while (currentPosition1 == 0 and currentPosition2 == 0): 
      for player in nameList: 
       RollDice()# How do I run this function which I've created above? 
      print("\n") 
     while (currentPosition1 <= 105 or currentPosition2 <= 105): 
      for player in nameList: 
       RollDice()# How do I run this function which I've created above? 
       CheckForLadder()# How do I run this function which I've created above? 
       CheckForSnake()# How do I run this function which I've created above? 
      print("\n") 

      if currentPosition1 >= 100: 
       print ("\n") 
       print ("Huuuuray! Winner is ", player1) 
       print ("Press any key to exit") 
       break 

      if currentPosition2 >= 100: 
       print ("\n") 
       print ("Huuuuray! Winner is ", player2) 
       print ("Press any key to exit") 
       break 
main() 
+2

這些不應該是方法和GameOn類是沒有意義的;去掉它。 –

回答

1

如果你創建了一個類,你必須先添加self參數,就像這樣:

class GameOn(): 

    def RollDice(self): 
    ... 

    def CheckForLadder(self): 
    ... 

    def CheckForSnake(self): 
    ... 


    def main(self): 
     while (currentPosition1 == 0 and currentPosition2 == 0): 
      for player in nameList: 
       self.RollDice() 
      print("\n") 

     while (currentPosition1 <= 105 or currentPosition2 <= 105): 
      for player in nameList: 
       self.RollDice() 
       self.CheckForLadder() 
       self.CheckForSnake() 
      print("\n") 

      if currentPosition1 >= 100: 
       print ("\n") 
       print ("Huuuuray! Winner is ", player1) 
       print ("Press any key to exit")#finish this 
       break 

      if currentPosition2 >= 100: 
       print ("\n") 
       print ("Huuuuray! Winner is ", player2) 
       print ("Press any key to exit")#finish this 
       break 

game = GameOn() 
game.main() 
+0

非常感謝你的伴侶,我想這是一個非常愚蠢的問題,我知道有一種方法可以做到,只是不知道如何...再次感謝! – greenGremlin

+0

@greenGremlin加油!沒有什麼值得感謝的!我一直都很喜歡幫忙。別擔心,大家從一開始就是正常的,不知道所有事情:) –

相關問題