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

我得到的错误是我的被标记为无效语法,所以我发现了这个: 创建类实例时的无效语法

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

所以我发现这个NameError: name 'Game' is not defined, but it is but, 我真的要接受吗self.play_game=play_game

课外???

好吧,现在看起来像这样:

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我选择删除g = class playGame()

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

--

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

  1. 问我网格的大小。

  2. 问我网格中有多少地雷。

  3. 玩。

  4. 结束。

标签: python

解决方案


你肯定在基础知识方面苦苦挣扎。第二次尝试很好,但是:

  1. 您必须从__init__函数中删除嵌套函数play_game
  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() 

推荐阅读