[英]IndexError I just can't figure out (Python)
我正在尝试通过 python 中的文本制作扫雷游戏。当我尝试绘制小数字时出现此错误。 也许我这样做的方式效率低下,但我不明白为什么会抛出这个错误。 我一直在尝试修改代码,但似乎没有任何效果。 有谁知道为什么它不起作用?
import random
minenum = 12
gridsize = 10
grid = [[0] * gridsize for _ in range(gridsize)]
def setup():
global grid
global minecount
for i in range(minenum):
x = random.randrange(0,10)
y = random.randrange(0,10)
grid[y][x] = "m"
xpos = 0
ypos = 0
for n in range(10):
for z in range(10):
count = 0
try:
if grid[ypos + 1][xpos] == "m":
count += 1
except:
pass
try:
if grid[ypos + 1][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos + 1][xpos - 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos - 1] == "m":
count += 1
except:
pass
try:
if grid[ypos - 1][xpos] == "m":
count += 1
except:
pass
try:
if grid[ypos][xpos + 1] == "m":
count += 1
except:
pass
try:
if grid[ypos][xpos - 1] == "m":
count += 1
except:
pass
grid[ypos][xpos] = count
xpos += 1
ypos += 1
def printBoard():
for i in range(10):
print(' '.join(str(v) for v in grid[i]))
setup()
printBoard()
[编辑]
这是错误:
Traceback (most recent call last):
File "main.py", line 74, in <module>
setup()
File "main.py", line 63, in setup
grid[ypos][xpos] = count
IndexError: list assignment index out of range
您的代码无法正常工作,因为您在增加ypos
时从未重置xpos
,因此您的索引看起来像这样(对于 gridsize = 4):
0 0
1 0
2 0
3 0
4 1
5 1
6 1
7 1
8 2
而不是你的意图,即
0 0
1 0
2 0
3 0
0 1
1 1
2 1
3 1
0 2
每当您执行 ypos += 1 时,您都应该添加 xpos = 0
xpos += 1
ypos += 1
xpos = 0
您的代码也可以使用一些清理:
import random
def setup(minecount, gridsize):
grid = [[0] * gridsize for _ in range(gridsize)]
for _ in range(minecount):
x = random.randrange(0,gridsize)
y = random.randrange(0,gridsize)
grid[y][x] = "m"
for xpos in range(gridsize):
for ypos in range(gridsize):
count = 0
if ypos + 1 < 10 and grid[ypos + 1][xpos] == "m":
count += 1
if ypos + 1 < 10 and xpos +1 < 10 and grid[ypos + 1][xpos + 1] == "m":
count += 1
if ypos + 1 < 10 and xpos - 1 >= 0 and grid[ypos + 1][xpos - 1] == "m":
count += 1
if ypos - 1 >= 0 and xpos + 1 < 10 and grid[ypos - 1][xpos + 1] == "m":
count += 1
if ypos - 1 >= 0 and xpos -1 >= 10 and grid[ypos - 1][xpos - 1] == "m":
count += 1
if ypos - 1 >= 0 and grid[ypos - 1][xpos] == "m":
count += 1
if xpos + 1 < 10 and grid[ypos][xpos + 1] == "m":
count += 1
if xpos - 1 >= 0 and grid[ypos][xpos - 1] == "m":
count += 1
grid[ypos][xpos] = count
return grid
def printBoard(grid):
for i in range(10):
print(' '.join(str(v) for v in grid[i]))
minecount = 12
gridsize = 10
grid = setup(minecount, gridsize)
printBoard(grid)
甚至不必更改逻辑,只需将幻数切换为适当的参数即可。 在计算相邻炸弹时,您还会覆盖所有“m”单元格,您可能希望避免这种情况。
您必须在 setup() function 的末尾将零分配给 xpos:
ypos += 1
xpos = 0
如果您在 grid[ypos][xpos] = count 之前添加一个 print(count) ,您将看到您有 11 个 count 实例,但 grid 只有 10 个,这就是原因。
你添加到 ypos 和 xpos 即使它是最大的,下面的快速修复但可以更好:
print(count)
grid[ypos][xpos] = count
if xpos < gridsize - 1:
xpos += 1
if ypos < gridsize - 1:
ypos += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.