0
import pygame
import player

play = player.player()
pygame.init()
time = pygame.time.Clock()
key = 0
move = ""
List =[]
pygame.display.set_mode((100, 100))
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            time_down = pygame.time.get_ticks()
            if event.key == pygame.K_LEFT:
                move = "l"
            if event.key == pygame.K_RIGHT:
                move = "r"
            if event.key == pygame.K_DOWN:
                move = "d"
                print('UNTEN')
            if event.key == pygame.K_UP:
                move = "u"
            if event.key == pygame.K_ESCAPE:
                break
            else:
                continue
        key += 1
        play.handlerevent(event)
        if event.type == pygame.KEYUP:
            time_elapsed = (pygame.time.get_ticks() - time_down) / 1000.0
            print("Nummer: ", key, "Zeit: ", time_elapsed)
            tmp = (move, key)
            List.extend(tmp)

Hello I'm new here and would like to know, why my for-loop doesn't react to the continue. It goes into the else branch. But just ignores the continue.

2
  • 3
    What makes you think continue is being ignored?
    – chepner
    Jan 9, 2020 at 22:09
  • For example if I press "a" "b" or "c", My Code below is still beeing executed
    – lucas
    Jan 9, 2020 at 22:15

1 Answer 1

3

I suspect you have it backwards: continue is being executed when it shouldn't be.

You need to use elif for your sequence of conditions. Your else: block is only associated with the last if condition. So if the key is K_LEFT it will go into the else: block and continue, instead of executing the rest of the loop.

            if event.key == pygame.K_LEFT:
                move = "l"
            elif event.key == pygame.K_RIGHT:
                move = "r"
            elif event.key == pygame.K_DOWN:
                move = "d"
                print('UNTEN')
            elif event.key == pygame.K_UP:
                move = "u"
            elif event.key == pygame.K_ESCAPE:
                break
            else:
                continue

This way, the else: block will be executed only when none of the if conditions succeeded.

4
  • i tried it and it makes no difference, I also did a print in the else case, the print is being executed but the continue isn't.
    – lucas
    Jan 9, 2020 at 22:23
  • the same thing happens to my break
    – lucas
    Jan 9, 2020 at 22:24
  • Are you sure you understand what continue does? It means it skips over everything starting with key += 1, and goes to the next event.
    – Barmar
    Jan 9, 2020 at 22:25
  • than its my bad sorry, i thought the contnue would go to the next for loop
    – lucas
    Jan 9, 2020 at 22:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.