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()