2

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 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that - I feel a bit stupid that I didn't notice that at first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.