【问题标题】:Accessing Variable in Loop在循环中访问变量
【发布时间】: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


【解决方案1】:

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

for a in across:
    print(a)

我使用print 作为for 循环的示例。此外,如果您使用的是 python 2,则应使用 print a 而不是 print(a)

【讨论】:

    【解决方案2】:

    我认为您的意思是添加across0across1across2across3,而不仅仅是简单的across

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

    【讨论】:

      【解决方案3】:

      您正在尝试将变量 across 添加到 node_counter,但您只定义了变量 across0across1、...这就是您收到错误的原因。

      还有一些其他错误

      • 将交叉值存储在数组数组中
      • 将外部循环中的 60 更改为 4(最好是对边的长度)
      • 更改内部循环以使用数组长度也是明智的
      • across 更改为使用故障线上的game_counter 进行索引。
      • if 行永远不会被执行,所以你会陷入无限循环。将 game_counter 增量移到内部循环之外。

      这给出了以下代码

      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
      

      但你可能最好使用 for 循环,至少对于内部循环:

      for item in across[game_counter]:
          ...
          ... (item + node_counter) ...
      

      【讨论】:

      • 是的 - 如何根据计数器的值访问 cross0、cross1 和 cross3?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      相关资源
      最近更新 更多