user2512696
user2512696

Reputation: 325

Accessing Variable in Loop

I'm writing a simple python program using a double loop to print out an html page with a unique set of coordinates. I'm getting an error because it cannot recognize the variable where I have (across + node_counter) in the loop. How do I do this so it gets the variable declared at the top?

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

Upvotes: 0

Views: 92

Answers (3)

northben
northben

Reputation: 5588

It looks like you're trying to iterate over your "across" variables. Maybe you mean something like this instead: across = ['72...', '143...']. Then you can iterate over across with a for loop:

for a in across:
    print(a)

I'm using print as an example for what the for loop would look like. Also, if you're using python 2, you would use print a instead of print(a).

Upvotes: 1

gilbo
gilbo

Reputation: 173

You are trying to add variable across to node_counter, but you've only defined variables across0, across1, ... That's why you're getting the error.

There are a few other errors too

  • store the across values in an array of arrays
  • change 60 in the outer loop to 4 (better would be length of across)
  • changing the inner loop to use the array length is also wise
  • change across to be indexed using the game_counter on your trouble line.
  • the if line will never be executed, so you'll get stuck in an infinite loop. Move the game_counter increment outside the inner loop.

This gives the following code

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

But you'd probably be better off with for loops, at least for the inner loop:

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

Upvotes: 0

user44484
user44484

Reputation:

I think you mean to add across0, across1, across2, and across3, not just plain across

Also, you don't need those continue statements.

Upvotes: 0

Related Questions