Python Skipping lines after a nested for loop

Viewed 75

In Python, I have defined a subroutine that is meant to take a list of strings and display them, through PyGame, on seperate Y-axis values. The code is as follows:

def blittext(list): # expects the list from the displaytext() function
    for i in list:
        z = 490
        text = ""
        for letter in i:
            #print("blitting")
            text += letter
            textFont = pygame.font.Font(os.path.join("InconsolataR.ttf"), 20)
            textblit = textFont.render(text, True, (255,255,255))
            display.blit(textblit, (400, z))
            pygame.display.update()
        z += 40

The line in question that is being skipped over is the last line:

z += 40

Which is supposed to add an increment of 40 after each line passes, but instead the function continues on as if the variable hadn't changed. I can't imagine this being an error of python, but rather a fundamental of nested states I've forgotten.

1 Answers

The line is not skipped, but z is initialized continuously at the begin of the same loop:

z = 490

Move z = 490 out of the loop:

def blittext(list): # expects the list from the displaytext() function

    z = 490 # <---- ADD 

    for i in list:

        # z = 490 <---- DELETE

        text = ""
        for letter in i:
            #print("blitting")
            text += letter
            textFont = pygame.font.Font(os.path.join("InconsolataR.ttf"), 20)
            textblit = textFont.render(text, True, (255,255,255))
            display.blit(textblit, (400, z))
            pygame.display.update()
        z += 40
Related