python - 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()
所以无效的语法消失了。(即使我不同意这是答案,但是)
--
所以我不知道在这里做什么。我想让它工作。如果它应该正常运行,它应该:
问我网格的大小。
问我网格中有多少地雷。
玩。
结束。
解决方案
你肯定在基础知识方面苦苦挣扎。第二次尝试很好,但是:
- 您必须从__init__函数中删除嵌套函数play_game。
- 更改对象初始化代码。
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()
推荐阅读
- c++ - 构建 qt 6.1.2 静态并使 qmysql 工作的正确方法
- cassandra - 我们如何根据时间戳列过滤行?
- angular - Angular Reactive Form - 在提交时将电子邮件地址转换为小写
- reactjs - 在 nextjs 之前加载启动画面
- python - Keras 模型适合形状错误的数据
- python - 为什么 if 语句正在执行但条件为假?
- docker - Logstash 未使用 9092 以外的端口从在 docker(compose) 内运行的 kafka 接收数据
- c++ - 将捕获传递给 lambda
- css - 在 Quasar Table 中设置内容位置
- javascript - 防止使用 iframe 的尺寸调整 iframe 内容的大小,但始终显示完整的内容