【问题标题】:How do I run functions inside my main function in python?如何在 python 的主函数中运行函数?
【发布时间】:2017-11-27 18:46:50
【问题描述】:

请帮助我编写编程作业的代码。我正在尝试在运行主游戏的 main() 函数中运行 RollDice() 函数、CheckForLadders() 函数和 CheckForSnakes() 函数。我已经定义了所有的全局变量,比如 currentPosition1、currentPosition2、ladderList、snakesList 和 nameList 等。

当我运行我的代码时,它说 RollDice() 没有定义 - 这是我在我的 main 中调用的初始函数。所以基本上,我将如何在我的代码上下文中调用这些函数?我认为将上述所有 4 个函数放在一个类 (gameOn) 中意味着它们可以相互访问,本质上成为方法。但我想我只是没有正确执行这个。

我知道我的逻辑是正确的,但我只是 python/编码的新手。请帮忙!

类游戏开启:

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

【问题讨论】:

  • 这些不应该是方法,GameOn类毫无意义;删除它。

标签: python function class methods pygame


【解决方案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()

【讨论】:

  • 非常感谢你,伙计,我想这是一个非常愚蠢的问题,我知道有办法做到这一点,只是不知道如何......再次感谢!
  • @greenGremlin 加油!没有什么可感谢的!我一直喜欢提供帮助。而且不用担心,大家都是从头开始的,不是什么都知道是正常的:)
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 1970-01-01
相关资源
最近更新 更多