Bitte helfen Sie mit meinem Code für eine Programmieraufgabe. Ich versuche, die RollDice() -Funktion, CheckForLaddders() -Funktion & CheckForSnakes() -Funktion innerhalb meiner main() -Funktion, die das Hauptspiel ausführen wird. Ich habe bereits alle globalen Variablen wie currentPosition1, currentPosition2, ladderList, snakesList & nameList usw. definiert.Wie führe ich Funktionen in meiner Hauptfunktion in Python aus?
Wenn ich meinen Code ausführen, sagt es RollDice() ist nicht definiert - das ist die erste Funktion, die ich in meinem Haupt aufrufen. Also, wie würde ich diese Funktionen im Kontext meines Codes nennen? Ich dachte, alle 4 der oben genannten Funktionen in eine Klasse (gameOn) zu setzen, würde bedeuten, dass sie aufeinander zugreifen könnten und im Wesentlichen zu Methoden werden. Aber ich denke, ich mache das nicht richtig.
Ich weiß meine Logik ist richtig, aber ich bin nur ein Neuling zu Python/Codierung .. bitte helfen!
Klasse 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()
Diese sollten diese Methoden nicht sein, und die GameOn Klasse ist sinnlos ; entfernen Sie es. –