[英]' ValueError: too many values to unpack '
I'm trying to write a basic version of minesweeper.You get the number of rows, columns,bombs and their location from the user.我正在尝试编写扫雷的基本版本。您可以从用户那里获得行数、列数、炸弹及其位置。 you should count number of bombs surrounding each element.
您应该计算每个元素周围的炸弹数量。 consider the example below: input
考虑下面的例子:输入
4 3
5
1 1
4 3
1 3
4 2
3 2
output is: output 是:
* 2 *
2 3 2
2 * 3
2 * *
Here's my code:这是我的代码:
n, m = [int(x) for x in input().split()]
k = int(input()) #number of bombs
#location of bombs
bomb = []
for i in range(k):
r,c = [int(r) - 1 for r in input().split()]
bomb.append([r,c])
#making the grid
map = []
for x in range(n):
map.append([])
for y in range(m):
map[x].append(0)
#locate bombs
for i, j in bomb:
map[i][j] = '*'
#count the bombs
Ns = 0
for i, j in map:
for x in range(-1, 2):
for y in range(-1, 2):
if map[i + x][j + y] == '*':
Ns += 1
map[i][j] = Ns
print(map)
My problem is finding bombs in surrendering.我的问题是在投降时找到炸弹。 I get
ValueError: too many values to unpack for line 25
.我得到
ValueError: too many values to unpack for line 25
。 Any idea how can i fix it?知道我该如何解决吗?
for i, j in map:
is invalid.是无效的。
map
is a list of lists, something akin to a 4x3 array. map
是一个列表列表,类似于 4x3 数组。
You seem to be trying to iterate through the indices of map
, and have confused those with the list contents.您似乎正在尝试遍历
map
的索引,并将这些索引与列表内容混淆。 Try尝试
for i in range(len(map)):
for j in range(len(map[i])):
...
There's two potential problems in your code.您的代码中有两个潜在问题。
Firstly, your map actually has 3 values to unpack for each value (since your m is 3), rather than two.首先,您的 map 实际上有 3 个值要为每个值解包(因为您的 m 是 3),而不是两个。 This is the line that you need to fix:
这是您需要修复的行:
for i, j in map:
Instead, what you need is probably to unpack each value for map, like so:相反,您可能需要解压缩 map 的每个值,如下所示:
for x in range(len(map)):
for y in range(len(map[i])):
square = map[x][y]
Secondly, your code for checking the surroundings can run into issues because you simply decrement/increment the x and y of the mine by 1, this will make your code try to find indexes in the map that doesn't exist.其次,您检查环境的代码可能会遇到问题,因为您只需将矿井的 x 和 y 减/增 1,这将使您的代码尝试在 map 中查找不存在的索引。 Eg, if x is 2, incrementing by 1 will give you 3. breaking your list.
例如,如果 x 为 2,则增加 1 将为您提供 3. 破坏您的列表。
What you probably want is:你可能想要的是:
for i in range(max(0,x-1),min(x+1,n-1)+1):
for j in range(max(0,y-1),min(y+1,m-1)+1):
This only takes in the values that are both in the surroundings, and do not violate the array bounds.这只接受环境中的值,并且不违反数组边界。 Max(0, x-1) prevents us from giving negative array bounds, and min(x+1, n-1) prevents us from going over the max array bounds.
Max(0, x-1) 阻止我们给出负数组边界,而 min(x+1, n-1) 阻止我们超过最大数组边界。 Similarly so for y.
同样,对于 y。
Combined, the code would probably look something like this:结合起来,代码可能看起来像这样:
#count the bombs
for x in range(len(map)):
for y in range(len(map[i])):
# if x,y is not a mine
if map[x][y] != '*':
# check the surroundings of x,y for mines
Ns = 0
for i in range(max(0,x-1),min(x+1,n-1)+1):
for j in range(max(0,y-1),min(y+1,m-1)+1):
# if this surrounding block is a mine, add to Ns
if (map[i][j]) == '*':
Ns += 1
print("Not safe: " + str(Ns))
# append the Ns number to the x,y block
map[x][y] = Ns
for layer in map:
print(layer)
Outputs:输出:
['*', 2, '*']
[2, 3, 2]
[2, '*', 3]
[2, '*', '*']
Note that this is definitely not the most optimized solution for the problem;请注意,这绝对不是该问题的最优化解决方案; the best way is to save values as we go along so we don't have to check all the 3 to 8 surrounding tiles for every tile, but hopefully this helps you understand the base case for dealing with minesweeper.
最好的方法是保存我们 go 的值,这样我们就不必为每个图块检查所有 3 到 8 个周围的图块,但希望这可以帮助您了解处理扫雷的基本情况。
Hope that helps!希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.