Python 在循环中访问变量

Python 在循环中访问变量,python,variables,while-loop,global-variables,counter,Python,Variables,While Loop,Global Variables,Counter,我正在编写一个简单的python程序,使用双循环打印出一个带有唯一坐标集的html页面。我得到一个错误,因为它无法识别我在循环中的变量(跨+节点\计数器)。我如何做到这一点,以便它在顶部声明变量 game_counter = 0 across0 = '72,70,97,70,97,61,116,71,97,83,97,75,72,75' across1 = '143,70,168,70,168,61,187,71,168,83,168,75,143,75' across2 = '212,70,

我正在编写一个简单的python程序,使用双循环打印出一个带有唯一坐标集的html页面。我得到一个错误,因为它无法识别我在循环中的变量(跨+节点\计数器)。我如何做到这一点,以便它在顶部声明变量

game_counter = 0

across0 = '72,70,97,70,97,61,116,71,97,83,97,75,72,75'
across1 = '143,70,168,70,168,61,187,71,168,83,168,75,143,75'
across2 = '212,70,237,70,237,61,256,71,237,83,237,75,212,75'
across3 = '283, 70, 308, 70, 309, 61, 327, 71, 308, 83, 308, 75, 283, 75'

while game_counter <60:
    text_file.write("<!-- These are the image maps for game " + str(game_counter) + " -->\n\n")
    node_counter = 0

    while node_counter < 15:
        placeholder_string = ""
        placeholder_string += '<area shape="poly" coords = "' + (across + node_counter) + '" href="#"/>\n'
        text_file.write(placeholder_string)
        node_counter += 1
        if node_counter == 15:
            game_counter += 1
game\u计数器=0
across0='72,70,97,61116,71,97,83,97,75,72,75'
across1='143,70168,70168,61187,71168,83168,75143,75'
across2='212,70237,70237,61256,71237,83237,75212,75'
across3='283,70,308,70,309,61,327,71,308,83,308,75,283,75'

当游戏计数器时,我想你的意思是添加
交叉0
交叉1
交叉2
,和
交叉3
,而不仅仅是简单的
交叉


另外,您不需要这些
continue
语句。

您试图将变量
添加到
节点计数器
,但您只定义了变量
跨0
跨1
。。。这就是为什么你会出错

还有一些其他的错误

  • 将跨值存储在数组数组中
  • 将外环中的60更改为4(最好是交叉长度)
  • 更改内部循环以使用数组长度也是明智的
  • 使用故障线路上的
    游戏计数器
    更改为索引
  • if
    行永远不会执行,因此您将陷入无限循环。将游戏计数器增量移出内循环
这将给出以下代码

across = [[72,70,97,70,97,61,116,71,97,83,97,75,72,75],
    [143,70,168,70,168,61,187,71,168,83,168,75,143,75],
    [212,70,237,70,237,61,256,71,237,83,237,75,212,75],
    [283, 70, 308, 70, 309, 61, 327, 71, 308, 83, 308, 75, 283, 75]]

while game_counter < len(across):
    text_file.write("<!-- These are the image maps for game " + str(game_counter) + " -->\n\n")
    node_counter = 0

    while node_counter < len(across[game_counter]):
        placeholder_string = ""
        placeholder_string += '<area shape="poly" coords = "' + (across[game_counter][node_counter] + node_counter) + '" href="#"/>\n'
        text_file.write(placeholder_string)
        node_counter += 1

    game_counter += 1

看起来您正在尝试迭代“跨”变量。也许你的意思是这样的:
cross=['72…','143…']
。然后,您可以使用
for
循环在
上进行迭代:

for a in across:
    print(a)

我正在使用
print
作为示例,说明
for
循环的外观。此外,如果您使用的是Python2,您将使用
print a
而不是
print(a)

您不需要在循环结束时放置
continue
,以使其进入下一个迭代。这在循环本身中是隐含的。在
交叉+node_计数器中
应该是什么?您从未定义过名为跨
的变量。这正是我所困惑的-我试图访问顶部声明的across0、across1等。您能给我们一个例子说明跨+节点计数器
可以是什么吗?我的意思是,您需要哪一个?按什么顺序?是的-如何根据计数器的值访问0、1和3?
for a in across:
    print(a)