Python Forum
[PyGame] Match statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] Match statement (/thread-40068.html)



Match statement - Fabrizio_fg - May-27-2023

I' m tryng to use the match statement to manage different event.type but givi me this error:

Output:
File "Main.py", line 34 match event.key(): ^ SyntaxError: invalid syntax
the code is :

for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:

            match event.key():

                case pygame.K_DOWN:
                    print("giu")

                case pygame.K_UP:
                    print("su")

                case pygame.K_LEFT:
                    print("sinistra")

                case pygame.K_RIGHT:
                    print("destra")
what am I doing wrong?


RE: Match statement - deanhystad - May-27-2023

Are you using Python 3.10 or newer?


RE: Match statement - Fabrizio_fg - May-27-2023

a little bit older LOL
2.7.15


RE: Match statement - deanhystad - May-27-2023

No match statement for you. Why are you using such an old version?


RE: Match statement - Fabrizio_fg - May-27-2023

I understand, i know i' m on a very old version of python, that because
i' m using fedora linux 26 a very old version, the new version is fedora 38 but i' m lazy.

Wall


RE: Match statement - buran - May-27-2023

The "canonical" way to do that was/is using dict

action = {pygame.K_DOWN:"giu",
          pygame.K_UP:"su",
          pygame.K_LEFT:"sinistra",
          pygame.K_RIGHT:"destra"}

print(action[event.key()])