Python:无法使我的代码工作.我的课程没有定义,或者我的呼叫未定义



我正在做一个清扫器游戏,这是整个代码:https://codeshare.io/5zxyxw

我正在尝试在第82行(从整个代码)上进行其他代码来制作这项工作:

g=class playGame(object):
    """Make the game play"""
    def __init__(self, play_game):
        def play_game(self, play_game):
            self.play_game = play_game
            grid_size = int(input("Choose the Width of the board: "))
            num = int(input("Choose the number of mines: "))
            self.mines = mines
            mines = place_mines(grid, mines)
            grid1 = grid(grid_size, self.mines)
            while not gameOver:
                print(grid)
                print("Make your move:")
                x = int(input("x: "))
                y = int(input("y: "))
                grid.makeMove(x, y)
                gameOver = grid.hitMine(x, y)
                if grid.isWinner() and gameOver == False:
                    gameOver = True
                    winner = True
                    print(grid)
                    if winner:
                        print("Congratulations. You Win!")
                    else:
                        print("You hit a mine. Game Over!")
g.play_game()

我遇到的错误是我的 class 被标记为无效的语法,因此我发现了以下内容:创建类实例class

时,我发现了这一点:

,但据我所知,play_game()是完全向右的。即使我添加了一个文档字符串,它仍然抱怨。

所以我找到了这个名字:名称"游戏"不是定义的,但是,但是,我真的要服用 self.play_game=play_game

班级外???

okey现在看起来像这样:

class playGame(object):
    def __init__(self, play_game):
        def play_game(self, play_game):
            #self.play_game = play_game
            grid_size = int(input("Choose the Width of the board: "))
            num = int(input("Choose the number of mines: "))
            self.mines = mines
            mines = place_mines(grid, mines)
            grid1 = grid(grid_size, self.mines)
            while not gameOver:
                print(grid)
                print("Make your move:")
                x = int(input("x: "))
                y = int(input("y: "))
                grid.makeMove(x, y)
                gameOver = grid.hitMine(x, y)
                if grid.isWinner() and gameOver == False:
                    gameOver = True
                    winner = True
                    print(grid)
                    if winner:
                        print("Congratulations. You Win!")
                    else:
                        print("You hit a mine. Game Over!")
self.play_game = play_game
play_game()

然后我得到了:

    self.play_game = play_game
NameError: name 'play_game' is not defined

注意!我选择在g = class playGame()

中删除g

因此,无效的语法消失了。(即使我不同意这是答案,但是)

-

所以我不知道在这里做什么。我想让它起作用。如果应该正确运行,则应:

  1. 问我网格的大小。

  2. 问我网格中有多少矿。

  3. 玩。

  4. 结束。

您肯定在基础知识上挣扎。第二次尝试很好,但是:

  1. 您必须删除嵌套函数 PLAY_GAME 来自 __ INIT __ INT __ INT __ 函数。
  2. 更改对象初始化代码。

class PlayGame(object): # class names should start with big letter
    def play_game(self):
        grid_size = int(input("Choose the Width of the board: "))
        num = int(input("Choose the number of mines: "))
        self.mines = mines
        mines = place_mines(grid, mines)
        grid1 = grid(grid_size, self.mines)
        while not gameOver:
            print(grid)
            print("Make your move:")
            x = int(input("x: "))
            y = int(input("y: "))
            grid.makeMove(x, y)
            gameOver = grid.hitMine(x, y)
            if grid.isWinner() and gameOver == False:
                gameOver = True
                winner = True
                print(grid)
                if winner:
                    print("Congratulations. You Win!")
                else:
                    print("You hit a mine. Game Over!")
game = PlayGame() # this line would call your __init__ function
game.play_game() 

您需要实例化类的对象。使用更新的代码,定义了类后,下一步该怎么办?显然,不能只是让它坐在那里,您必须将对象(变量)实例化。因此,您已经定义了类playGame

# Instantiate it to whichever variable/object here
myInstance = PlayGame()

这将实例化游戏类并将其分配给对象myInstancedef __init__中定义的所有内容也将执行。

# Then execute any function(s) outside of `__init__`
# With your code, since everything is inside of `__init__`, you don't need anything else
# With Dawid's code, you need another line like so:
myInstance.play_game()

最新更新