r/PythonLearning • • 14d ago

Help Request Else statement not working

Post image

Hello, my else statement is not working- the if statement its inside is, did i mess up the indents again or is it something else?

also would a display flip work to change the image back to the other image in the else statement?
any help is nice!

26 Upvotes

12 comments sorted by

17

u/gitChara 14d ago

you wrote "img - pygame.image.load("ragebait.png")" with a " - ", instead of a " = ".
It should be "img = pygame.image.load("ragebait.png")"

2

u/TheCarFamily 14d ago

oh your kidding i was that dumb? thanks!

5

u/rupertavery64 14d ago

You probably don't want to call image.load() every time as this willnload the image from the disk and create a new object every call.

Load it up front and store it in a variable. Assign that variable to img

3

u/alwaysasillyplace 13d ago

This is why Peer review is good.

1

u/a_cute_tarantula 13d ago

Install pyright extension for vscode. It should come with the standard python extension pack. You should have error highlighting automatically for this kind of stuff

1

u/Most_Strike_93 12d ago

I would recommend ty now, and with ruff to format and lint

1

u/Dry-Bread9131 10d ago

You're not dumb at all. We've all made the same mistake with some character or other, and struggled just as much as you with finding it visually.

What you should do, as others have suggested, is use some kind of plugin for your IDE or editor to highlight and warn about syntactic issues like these. In this case I guess it should have warned about unassigned value of an operation (for whatever the - operator does for the pygame.image class.)

1

u/KidTempo 13d ago

You're double-indenting (which may just be a configuration setting of your IDE and probably not causing any strange behaviour).

If you're debugging, is the else block entered? Does "nono" get printed?

If the problem is that img is not loading, then it's because you're using - instead of = in the line after print("nono")

1

u/TheCarFamily 13d ago

it does not print, that is the problem

1

u/KidTempo 13d ago

Try replacing the start with the following:

if event.type == pygame.KEYDOWN
    print(f"{event.key=} :: {pygame.K_t=} :: {event.key == pygame.K_t}")
    if event.key == pygame.K_t:
        # continue the rest unchanged...

It should print what the event.key and pygame.K_t are each time, and whether they are equal. If the else block is never being triggered, then that would suggest that event.key always equals pygame.K_t

1

u/p1geondove 14d ago

it is indented one to far, but python can handle this luckily. about if display.flip() stores the last buffer: hmm maybe, why dont you try out? :)

import pygame
window = pygame.display.set_mode((500,500))
img = pygame.image.load("ragebait.png")

window.fill("grey10")
window.blit(img)
pygame.display.flip()

window.fill("grey10")
window.blit(img,(100,100))

while 1:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            pygame.display.flip()
        elif event.type == pygame.QUIT:
            exit()

https://github.com/pygame-community/pygame-ce/blob/main/src_c/display.c

was curios to find out why, but i think flip is just a historical name. the way i read it, theres no actual double buffer involved, it only draws to the surface whats in screen->buffer

1

u/thee_gummbini 13d ago

🫸tabs vs spaces

👉 4 spaces vs 8 spaces