2

Please help with my code for a programming assignment. I'm trying to run the RollDice() function, CheckForLaddders() function & CheckForSnakes() function inside my main() function which will run the main game. I have already defined all the global variables like currentPosition1, currentPosition2, ladderList, snakesList & nameList etc.

When i run my code it's saying RollDice() is not defined - which is the initial function I call inside my main. So basically, how would I call those functions within the context of my code? I thought putting all 4 of the above functions in a class (gameOn) would mean they could access each other, essentially becoming methods. But i guess I'm just not executing this correctly.

I know my logic is right, but I'm just a newbie to python/coding.. please help!

class 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()
1
  • 2
    These shouldn't be methods, and the GameOn class is pointless; remove it. Commented Jun 24, 2017 at 8:12

1 Answer 1

1

If you created a class, you must add the self parameter first, like this:

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much mate, I guess it was a pretty silly question and I knew there was a way to do it, just didn't know how... Thanks again !!
@greenGremlin Come on! there is nothing to thank about! I have always loved to help. And don't worry, everybody starts from the beginning, is normal not know everything :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.