Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Match statement
#1
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?
Reply
#2
Are you using Python 3.10 or newer?
Reply
#3
a little bit older LOL
2.7.15
Reply
#4
No match statement for you. Why are you using such an old version?
Reply
#5
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
Reply
#6
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()])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020