【发布时间】:2014-01-09 03:33:42
【问题描述】:
我正在编写一个简单的 python 程序,使用双循环打印出一个带有一组唯一坐标的 html 页面。我收到一个错误,因为它无法识别我在循环中拥有的变量(跨 + node_counter)。我该怎么做才能获得在顶部声明的变量?
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
【问题讨论】:
-
您无需在循环末尾添加
continue即可使其继续进行下一次迭代。这隐含在循环本身中。 -
across应该在across + node_counter中是什么?您从未定义过名为across的变量。 -
这就是我感到困惑的地方 - 我正在尝试访问顶部声明的 cross0、cross1 等。
-
您能给我们举个例子说明
across + node_counter可能是什么吗? -
我的意思是,你需要哪一个?按什么顺序?
标签: python variables while-loop global-variables counter