"unexpected argument"

Terry Reedy tjreedy at udel.edu
Tue Mar 9 19:58:26 EST 2021


On 3/9/2021 3:03 PM, Quentin Bock wrote:
> Error 1:
> Space Invaders.py:90: SyntaxWarning: "is" with a literal. Did you mean "=="?
>    if bullet_state is "fire":

Multiple string objects can have the same string value.  Just because 
bullet_state == 'fire' does not mean that it *is* the particular object 
being compared to.  Leaving testing aside, 'is' should only be used when 
the language mandates that there can only be one object with the value 
you want.  'is None' is the most common proper use.  Using 'is' when you 
should use '==' is a common and hard to understand beginner bug.


> Error 2:
> line 66, in <module>
>      if event.key == pygame.K_SPACE:
> AttributeError: 'Event' object has no attribute 'key'

>          # Check whether keystroke is being pressed
> 
>          if event.type == pygame.KEYDOWN:
>              if event.key == pygame.K_LEFT:
>                  player_X_change = -6
>              if event.key == pygame.K_RIGHT:
>                  player_X_change = 6
> 
>          if event.key == pygame.K_SPACE:
>              fire_bullet(player_X, bullet_Y)

For other keys, you check event.type first.  Here you do not.  At some 
point, you asked for the key for a non-key event.  Put this statement 
under the keyup or keydown event type check, depending on when you want 
fire to happen.

>          if event.type == pygame.KEYUP:
>              if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
>                  player_X_change = 0


-- 
Terry Jan Reedy



More information about the Python-list mailing list